在linux中,$*和$@有什么区别?

看了很多的文章,依然似懂非懂。现在,终于理解了它们两者之间的区别。

$*会把当前脚本的所有参数作为一个参数传递给子脚本。(在英文中,*字符有“所有”的意思)

$@会把当前脚本的所有参数分别作为一个参数传递给子脚本。(在英文中,@字符有“独立”的意思)

脚本start.sh的内容如下:

first.sh $* "this is the first shell"

second.sh $@ "this is the second shell"

执行命令 start.sh a b c d e f g,则实际执行的first.sh和second.sh脚本如下:

first.sh "a b c d e f g" "this is the first shell"(first.sh脚本只有2个参数)

second.sh a b c d e f g "this is the second shell"(second.sh脚本有8个参数)

在使用for命令时,$@传递给for命令多个参数,所以for命令迭代了多次,$*只给for命令传递了1个参数,所以for命令只迭代了1次。

猜你喜欢

转载自blog.csdn.net/zslin2011/article/details/83996982