首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在shell脚本中,$@是什么意思?

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

Stack Overflow用户
提问于 2012-04-03 21:28:31
回答 6查看 247.7K关注 0票数 271

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

例如:

代码语言:javascript
复制
umbrella_corp_options $@
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2012-04-03 21:30:24

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

例如,如果调用./someScript.sh foo bar,则$@将等于foo bar。

如果您这样做了:

代码语言:javascript
复制
./someScript.sh foo bar

然后在someScript.sh引用内部:

代码语言:javascript
复制
umbrella_corp_options "$@"

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

票数 336
EN

Stack Overflow用户

发布于 2012-04-03 22:26:30

$@与$*几乎相同,都意味着“所有命令行参数”。它们通常被用来简单地将所有参数传递给另一个程序(从而在另一个程序周围形成一个包装器)。

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

代码语言:javascript
复制
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.

示例:调用

代码语言:javascript
复制
wrapper "one two    three" four five "six seven"

将导致:

代码语言:javascript
复制
"$@": 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
票数 148
EN

Stack Overflow用户

发布于 2013-10-22 23:21:20

以下是命令行参数,其中:

$@ =将所有参数存储在字符串列表中

$* =将所有参数存储为单个字符串

$# =存储参数的数量

票数 51
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9994295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档