Bash 中的 $@ 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3898665/
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-09-09 19:44:24  来源:igfitidea点击:

What is $@ in Bash?

basharguments

提问by vecvan

I reckon that the handle $@in a shell script is an array of all arguments given to the script. Is this true?

我认为$@shell 脚本中的句柄是提供给脚本的所有参数的数组。这是真的?

I ask because I normally use search engines to gather information, but I cant google for $@and I have grown too custom to easily getting served everything.

我问是因为我通常使用搜索引擎来收集信息,但我不能谷歌搜索,$@而且我已经变得太习惯了,无法轻松获得所有内容。

回答by ghostdog74

Yes. Please see the man page of bash( the first thing you go to ) under Special Parameters

是的。请参阅特殊参数下的 bash 手册页(您访问的第一件事)

Special Parameters

The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.

*Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*"is equivalent to "$1c$2c...", where cis the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

@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).

特殊参数

shell 对几个参数进行了特殊处理。这些参数只能被引用;不允许分配给他们。

*扩展到位置参数,从 1 开始。当扩展发生在双引号内时,它扩展为单个单词,每个参数的值由 IFS 特殊变量的第一个字符分隔。也就是说,"$*"相当于"$1c$2c...",其中c是 IFS 变量值的第一个字符。如果未设置 IFS,则参数以空格分隔。如果 IFS 为空,则连接参数时不插入分隔符。

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

回答by Thomas Chilinski

Just from reading that i would have never understood that "$@"expands into a list of separate parameters. Whereas, "$*"is one parameter consisting of all the parameters added together.

只是从阅读我永远不会理解"$@"扩展为单独参数列表的内容。而,"$*"是一个参数,由所有参数加在一起组成。

If it still makes no sense do this.

如果仍然没有意义,请执行此操作。

http://www.thegeekstuff.com/2010/05/bash-shell-special-parameters/

http://www.thegeekstuff.com/2010/05/bash-shell-special-parameters/