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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9994295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:35:13  来源:igfitidea点击:

What does $@ mean in a shell script?

linuxbashshellunixsh

提问by trusktr

What does a dollar sign followed by an at-sign (@) mean in a shell script?

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

For example:

例如:

umbrella_corp_options $@

采纳答案by Har

$@is allof the parameters passed to the script.

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

For instance, if you call ./someScript.sh foo barthen $@will be equal to foo bar.

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

If you do:

如果你这样做:

./someScript.sh foo bar

and then inside someScript.shreference:

然后内部someScript.sh参考:

umbrella_corp_options "$@"

this will be passed to umbrella_corp_optionswith each individual parameter enclosed in double quotes, allowing to take parameters with blank space from the caller and pass them on.

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

回答by Christoffer Hammarstr?m

From the manual:

从手册:

@

Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" .... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

@

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

回答by glglgl

The usage of a pure $@means in most cases "hurt the programmer as hard as you can", because in most cases it leads to problems with word separation and with spaces and other characters in arguments.

$@在大多数情况下,使用 pure意味着“尽可能地伤害程序员”,因为在大多数情况下,它会导致单词分隔以及参数中的空格和其他字符出现问题。

In (guessed) 99% of all cases, it is required to enclose it in ": "$@"is what can be used to reliably iterate over the arguments.

在(猜测的)99% 的情况下,需要将其包含在":"$@"中,可以用来可靠地迭代参数。

for a in "$@"; do something_with "$a"; done

回答by Alfe

$@is nearly the same as $*, both meaning "all command line arguments". They are often used to simply pass all arguments to another program (thus forming a wrapper around that other program).

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

The difference between the two syntaxes shows up when you have an argument with spaces in it (e.g.) and put $@in double quotes:

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

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.

Example: Calling

示例:调用

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

will result in:

将导致:

"$@": 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

回答by Sameer Duwal

These are the command line arguments where:

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

$@= stores all the arguments in a list of string
$*= stores all the arguments as a single string
$#= stores the number of arguments

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

回答by Luis Lavaire

Meaning.

意义。

In brief, $@expands to the positional arguments passed from the caller to either a function or a script. Its meaning is context-dependent: Inside a function, it expands to the arguments passed to such function. If used in a script (not inside the scope a function), it expands to the arguments passed to such script.

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

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

$ ./my-sh "Hi!"
Hi!
$ put () ( echo "$@" )
$ put "Hi!"
Hi!


Word splitting.

分词。

Now, another topic that is of paramount importance when understanding how $@behaves in the shell is word splitting. The shell splits tokens based on the contents of the IFSvariable. Its default value is \t\n; i.e., whitespace, tab, and newline.

现在,在理解$@shell 中的行为时另一个最重要的主题是分。Shell 根据IFS变量的内容拆分标记。它的默认值为\t\n; 即,空格、制表符和换行符。

Expanding "$@"gives you a pristine copyof the arguments passed. However, expanding $@will not always. More specifically, if the arguments contain characters from IFS, they will split.

扩展"$@"为您提供了所传递参数的原始副本。然而,扩张$@并不总是如此。更具体地说,如果参数包含来自 的字符IFS,它们将拆分。



Most of the time what you will want to use is "$@", not $@.

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