shell编程----$@与$*的差别

摘于:

http://blog.chinaunix.net/uid-9160071-id-2435395.html

 

通过一段代码来看一下:

#!/bin/bash 
#the name is "test"
echo $@
echo $*
echo "$@"
echo "$*"
a1=($@)
b1=($*)
a2=("$@")
b2=("$*")
echo 'the number of parameters in $@ is '${#a1[*]}
echo 'the number of parameters in $* is '${#b1[*]} 
echo 'the number of parameters in "$@" is '${#a2[*]}
echo 'the number of parameters in "$*" is '${#b2[*]} 
exit 0

在终端中运行 sh test p1 p2 "p3 p4"
输出结果为:
p1 p2 p3 p4
p1 p2 p3 p4
p1 p2 p3 p4
p1 p2 p3 p4
the number of parameters in $@ is 4
the number of parameters in $* is 4
the number of parameters in "$@" is 3
the number of parameters in "$*" is 1

 

从上面的结果可以看出,$@,$*区别不是很大,最主要的区别是"$@","$*"。在"$@"中参数为分解为p1 p2 "p3 p4";在"$*"中参数则是一个整体"p1 p2 p3 p4"

posted on 2015-07-19 10:16  依风152  阅读(157)  评论(0编辑  收藏  举报