$ *和$ @之间的差 [英] The difference between $* and $@
问题描述
可能重复:结果
什么区别&ldquo的区别; $ @”的和“ $ * rdquo;的在bash?
有关年,几十个场合,我已经使用 $之间犹豫*
和 $ @
中shell脚本。看了一遍又一遍的Bash的联机帮助的适用部分,尝试过两种 $ *
和 $ @
,我更以下完全无法理解两个变量之间的应用程序的实际差别。你可以告诉我,好吗?
For years and on dozens of occasions, I have hesitated between the use of $*
and $@
in shell scripts. Having read the applicable section of Bash's manpage over and over again, having tried both $*
and $@
, I more or less completely fail to understand the practical difference of application between the two variables. Can you enlighten me, please?
我一直使用 $ *
最近,但不要问我为什么。我不知道为什么,因为我不知道为什么 $ @
即使存在,除非作为 $ *几乎确切的代名词
。
I have been using $*
recently, but don't ask me why. I don't know why, because I don't know why $@
even exists, except as an almost exact synonym for $*
.
有没有实际的区别吗?
(我个人倾向于使用猛砸,但仍不可知对于外壳的选择。我的问题是不特定为据我所知猛砸。)
(I personally tend to use Bash, but remain agnostic regarding the choice of shell. My question is not specific to Bash as far as I know.)
推荐答案
未加引号的,没有任何区别 - 他们扩展到所有的争论和他们相应的分割。所不同的引用来的时候。 $ @
扩展到正确引用参数和$ *
把所有的参数到一个单一的参数。借此例如:
Unquoted, there is no difference -- they're expanded to all the arguments and they're split accordingly. The difference comes when quoting. "$@"
expands to properly quoted arguments and "$*"
makes all arguments into a single argument. Take this for example:
#!/bin/bash
function print_args_at {
printf "%s\n" "$@"
}
function print_args_star {
printf "%s\n" "$*"
}
print_args_at "one" "two three" "four"
print_args_star "one" "two three" "four"
然后:
$ ./printf.sh
one
two three
four
one two three four
这篇关于$ *和$ @之间的差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!