bash – $@和“$@”之间有什么区别吗?

前端之家收集整理的这篇文章主要介绍了bash – $@和“$@”之间有什么区别吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Accessing bash command line args $@ vs $*4个
$@和“$@”之间有什么区别吗?

我知道非特殊字符可能存在差异,但带有输入参数的@符号呢?

是!
$cat a.sh
echo "$@"
echo $@

我们来运行吧:

$./a.sh 2 "3     4" 5
2 3     4 5                  # output for "$@"
2 3 4 5                      # output for $@  -> spaces are lost!

正如您所看到的,使用$@会使参数在用作参数时“丢失”某些内容.有关详细说明,请参见-for-example- I just assigned a variable,but echo $variable shows something else.

GNU Bash manual –> 3.4.2 Special Parameters开始:

@

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

猜你在找的Bash相关文章