- 论坛徽章:
- 0
|
看到<sams Teach yourself shell programming in 24 hours>里面关于参数章节的对$*和$@的解释如下
$*: All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.
$@: All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2
还有一段对$*的理解说明:
The main difference between these two is how they expand arguments. When $* is used, it simply expands each argument without preserving quoting. This can sometimes cause a problem. If your script is given a filename containing spaces as an argument,
mytar –t "my tar file.tar"
using $* would mean that the for loop would call tar three times for files named my, tar, and file.tar, instead of once for the file you requested: my tar file.tar.
By using $@, you avoid this problem because it expands each argument as it was quoted on the command line.
我的理解为按照mytar "my tar file.tar"的格式
使用$*将去掉参数中""的作用, 而把参数作为三个值, 而使用$@则保留""的作用, 将参数看作一个值.
我使用如下的例子验证
echo 'difference between $* and $@'
for FRUIT in $*
do
echo $FRUIT
done
for P in $@
do
echo $P
done
其运行结果是一样的.
是不是我的理解错了. 哪位仁兄帮忙解释一下. 另外, shell中对数组的引用也有*和@的区别. 是否和这个也一样?
我的环境是 Fedora core 1,
#bash --version
GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu)
Copyright (C) 2002 Free Software Foundation, Inc. |
|