$ ~ >cat testargs.sh
#!/bin/bashecho "you passed me" $*echo "you passed me" $@
其结果是:
$ ~> bash testargs.sh arg1 arg2
you passed me arg1 arg2
you passed me arg1 arg2
.之间的区别是什么?$*和$@? 何时使用前者,何时使用后者?
月关宝盒
浏览 915回答 3
3回答
四季花海
当引用特殊参数时,会出现差异。让我来说明不同之处:$ set -- "arg 1" "arg 2" "arg 3"$ for word in $*; do echo "$word"; donearg1arg2arg3$ for word in $@; do echo "$word"; donearg1arg2arg3$ for word in "$*"; do echo "$word"; donearg 1 arg 2 arg 3$ for word in "$@"; do echo "$word"; donearg 1arg 2arg 3关于引用的重要性的另一个例子:注意,在“arg”和数字之间有两个空格,但是如果我不能引用$word:$ for word in "$@"; do echo $word; donearg 1arg 2arg 3在巴什,"$@"是要迭代的“默认”列表:$ for word; do echo "$word"; donearg 1arg 2arg 3