$ @在shell脚本中意味着什么?

tru*_*ktr 155 unix linux bash shell sh

@在shell脚本中,一个美元符号后跟一个at符号()意味着什么?

例如:

umbrella_corp_options $@
Run Code Online (Sandbox Code Playgroud)

Har*_*Har 208

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

例如,如果你打电话./someScript.sh foo bar那么$@就等于foo bar.

如果你这样做:

./someScript.sh foo bar
Run Code Online (Sandbox Code Playgroud)

然后在内部someScript.sh参考:

umbrella_corp_options "$@"
Run Code Online (Sandbox Code Playgroud)

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

  • 如果用双引号书写,$ @是特殊的.然后它会产生一个引用值列表,在你的情况下,trusktr,在三个参数"foo","bar"和"boo far"中. (6认同)
  • 虽然通常都是这种情况,但`$ @*确实*不一定*来自传递给脚本的参数...例如; `set ab"x y"; printf'(%s)'"$ @"`输出`(a)(b)(x y)` (4认同)
  • 我更喜欢 Alfe 的回答,因为他给出了 `$@` 和 `$*` 之间的主要区别 (3认同)
  • 如果我做了`someScript.sh foo bar "boo far"`,$@ 会包含什么? (2认同)
  • http://teaching.idallen.com/dat2330/04f/notes/shell_variables.txt 看看这个。 (2认同)
  • 小挑剔:有隐式参数“$0”。`$@` 是从 `$1` 开始的所有参数 (2认同)

Alf*_*lfe 89

$@几乎相同$*,都意味着"所有命令行参数".它们通常用于简单地将所有参数传递给另一个程序(从而形成围绕其他程序的包装).

当你有一个带有空格的参数(例如)并放入$@双引号时,两个语法之间的差异会显示出来:

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.
Run Code Online (Sandbox Code Playgroud)

示例:致电

wrapper "one two    three" four five "six seven"
Run Code Online (Sandbox Code Playgroud)

将导致:

"$@": 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
Run Code Online (Sandbox Code Playgroud)

  • 不,他们不是.我在下面说了两行:"两者之间的区别......"为了得到短句并提高可读性,读者在作出判决之前必须阅读多个句子: - / (5认同)
  • 它们*不*相同,联机帮助页清楚说明了 $* 使用 IFS 的副作用——这不一定是空格。(如果它们相同,那么除了兼容性之外,提供两者都没有任何意义。) (2认同)
  • Alfe,Christoffer插入一个"差不多"的单词,在不牺牲任何简洁性或可读性的情况下,造成了一个与众不同的地方.事实上,我完全赞同这个答案(而不是被接受的答案)正是由于*差异的微妙强调*...... - 才意识到它几乎违背你的意愿!;) (2认同)

小智 32

这些是命令行参数,其中:

$@=将所有参数存储在string
$*= 的列表中将所有参数存储为单个字符串
$#=存储参数的数量

  • 在这种情况下,“列表”是什么? (3认同)

Lui*_*ire 19

意义。

简而言之,$@扩展为从调用者传递到函数脚本的参数。它的含义与上下文相关:在函数内部,它扩展为传递给该函数的参数。如果在脚本中(在函数外部)使用,它会扩展为传递给该脚本的参数。

$ cat my-script
#! /bin/sh
echo "$@"

$ ./my-script "Hi!"
Hi!
Run Code Online (Sandbox Code Playgroud)
$ put () { echo "$@"; }
$ put "Hi!"
Hi!
Run Code Online (Sandbox Code Playgroud)

* 注意:分词。

shell 根据IFS环境变量的内容分割令牌。它的默认值是 \t\n;即空白、制表符和换行符。扩展"$@"为您提供了所传递参数的原始副本。扩大$@也许不行。更具体地说,任何包含 中 存在的字符的参数IFS可能会分成两个或多个参数或被截断。

因此,大多数时候您想要使用的是"$@",而不是$@


glg*_*lgl 11

$@在大多数情况下,使用纯手段"尽可能地伤害程序员",因为在大多数情况下,它会导致单词分离以及参数中的空格和其他字符的问题.

在(猜测)99%的所有情况中,需要将其包含在":"$@"可用于可靠地迭代参数的内容.

for a in "$@"; do something_with "$a"; done
Run Code Online (Sandbox Code Playgroud)

  • 你的行可以写成:for a; 做某事_with"$ a"; 完成;-) (4认同)

Chr*_*röm 6

从手册:

@

从1开始扩展到位置参数.当扩展发生在双引号内时,每个参数都会扩展为单独的单词.也就是说,"$ @"相当于"$ 1""$ 2"....如果双引号扩展发生在一个单词中,则第一个参数的扩展与原始单词的开头部分连接,并且最后一个参数的扩展与原始单词的最后一部分连接在一起.当没有位置参数时,"$ @"和$ @扩展为空(即,它们被删除).