我一直在CentOS 7上处理一些脚本,有时我看到:
#!/bin/sh -
在第一行。查看
sh
的手册页,我在Special Parameters
下看到以下内容 - Expands to the current option flags as specified upon invocation,
by the set builtin command, or those set by the shell
itself (such as the -i option).
这到底是什么意思?什么时候需要使用此特殊参数选项?
最佳答案
您正在阅读的文档与您正在查看的命令行无关:它是指特殊变量。在这种情况下,如果运行echo $-
,您将看到“调用时指定的当前选项标志...”。
如果查看OPTIONS
手册页的bash
部分,您会发现:
-- A -- signals the end of options and disables further option processing.
Any arguments after the -- are treated as filenames and arguments. An
argument of - is equivalent to --.
换句话说,
-
的参数仅表示“此参数之后没有其他选项”。您经常会在需要避免以
-
开头的文件名被意外地当作命令选项的情况下使用此命令:例如,如果当前目录中有一个名为-R
的文件,则运行ls *
实际上将表现为ls -R
并产生递归列表,而ls -- *
不会特别对待-R
文件。在
#!
行中使用单破折号是为了安全起见。您可以阅读有关here的更多信息。关于shell - 在#!/bin/sh之后加破折号 '-',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57116144/