shell 脚本中的“set --”$progname”“$@””是什么意思?

Zij*_*gWu 5 linux shell

我目前从我们的 configure.ac 构建脚本中读取了这一行。我在谷歌上搜索过答案,但没有找到。

我认为它是 shell 脚本,但这意味着什么,尤其是对于--?

set -- "$progname" "$@"
Run Code Online (Sandbox Code Playgroud)

Ram*_*pte 11

来自help set

  --  Assign any remaining arguments to the positional parameters.
      If there are no remaining arguments, the positional parameters
      are unset.
Run Code Online (Sandbox Code Playgroud)

原因--是为了确保即使"$progname""$@"包含破折号,它们也不会被解释为命令行选项。

set更改存储在$@. 所以在这种情况下,它附加 "$progname"到脚本接收的位置参数的开头。


Jon*_*Lin 5

--是 bash 内置的,也是许多 unix 命令用来表示命令选项结束的东西。所以如果你有类似的东西:

grep -- -v file
Run Code Online (Sandbox Code Playgroud)

the-v不会被解释为 grep 选项,而是一个参数(因此您可以 grep for -v)。

$@是传递到脚本中的所有参数的列表(我假设该set命令是其中的一部分)。

确保--作为脚本一部分传入的任何选项都不会被解释为 的选项set,而是解释为变量表示的命令的选项$progname