根据此页面,$ @和$ *做几乎相同的事情:
The $@ holds list of all arguments passed to the script.
The $* holds list of all arguments passed to the script.
在搜索了Google的所有热门歌曲之后,我无法找到任何解释说明为什么会有2个看似重复的语法。
它们在我的脚本中似乎工作相同。
cat foo.sh
#!/bin/bash
echo The parameters passed in are $@
echo The parameters passed in are $*
./foo.sh herp derp
The parameters passed in are herp derp
The parameters passed in are herp derp
其他来源
bash.cyberciti.biz
他们不一样。$*
是单个字符串,而$@
实际上是一个数组。要查看区别,请执行以下脚本,如下所示:
> ./test.sh one two "three four"
剧本:
#!/bin/bash
echo "Using \"\$*\":"
for a in "$*"; do
echo $a;
done
echo -e "\nUsing \$*:"
for a in $*; do
echo $a;
done
echo -e "\nUsing \"\$@\":"
for a in "$@"; do
echo $a;
done
echo -e "\nUsing \$@:"
for a in $@; do
echo $a;
done
以下是这四种情况的说明和结果。
在第一种情况下,参数被视为一个长引号的字符串:
Using "$*":
one two three four
情况2(无引号)-字符串被for
循环分解成单词:
Using $*:
one
two
three
four
情况3-将$ @的每个元素都视为带引号的字符串:
Using "$@":
one
two
three four
最后一种情况-将每个元素都视为未加引号的字符串,因此最后一个元素再除以等于for three four
:
Using $@:
one
two
three
four
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句