$@ 在 shell 脚本中是什么意思?

新手上路,请多包涵

在 shell 脚本中,美元符号后跟 at 符号( @ )是什么意思?

例如:

 umbrella_corp_options $@

原文由 trusktr 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 998
2 个回答

$@ $* 相同,都表示“所有命令行参数”。它们通常用于简单地将所有参数传递给另一个程序(从而形成围绕该其他程序的包装器)。

当您有一个带有空格的参数(例如)并将 $@ 放在双引号中时,两种语法之间的差异就会出现:

 wrappedProgram "$@"
# ^^^ this is correct and will hand over all arguments in the way
#     we received them, i. e. as several arguments, each of them
#     containing all the spaces and other uglinesses they have.
wrappedProgram "$*"
# ^^^ this will hand over exactly one argument, containing all
#     original arguments, separated by single spaces.
wrappedProgram $*
# ^^^ this will join all arguments by single spaces as well and
#     will then split the string as the shell does on the command
#     line, thus it will split an argument containing spaces into
#     several arguments.

示例:调用

wrapper "one two    three" four five "six seven"

将导致:

 "$@": wrappedProgram "one two    three" four five "six seven"
"$*": wrappedProgram "one two    three four five six seven"
                             ^^^^ These spaces are part of the first
                                  argument and are not changed.
$*:   wrappedProgram one two three four five six seven

原文由 Alfe 发布,翻译遵循 CC BY-SA 4.0 许可协议

$@ 是传递给脚本的 所有 参数。

例如,如果您调用 ./someScript.sh foo bar 那么 $@ 将等于 foo bar

如果你这样做:

 ./someScript.sh foo bar

然后在里面 someScript.sh 参考:

 umbrella_corp_options "$@"

这将被传递给 umbrella_corp_options 每个单独的参数都用双引号括起来,允许从调用者那里获取带有空格的参数并传递它们。

原文由 Har 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题