$*:如果加" ",即"$*"则表示视所有该命令参数为一个单词,如果没有" ",那么该命令每一个参数
都视为独立的参数。比如传入的参数:1 2 3 4,$*:表示1 2 3 4;"$*":表示1,2,3,4;
$@:不管有" "没有" ",每个参数都是独立的,但是,如果传入的参数比如是:1 "2 3" 4,那就有点差异了,"$@": 表示1,2 3,4;而$@:表示1,2,3,4;
$#:表示参数个数。
例子:
[yangzheng@localhost shell_test]$ cat test.sh
echo
index=1
echo "Listing args with\"\$*\":"
for arg in "$*"
do
echo "Arg #$index=$arg"
let "index+=1"
done
echo "All the parameters is considered to be a word."
echo "==========================================================="
echo
index=1
echo "Listing arg with\"\$@\":"
for arg in "$@"
do
echo "Arg #$index=$arg"
let "index+=1"
done
echo "All the parameters is considered to be separated words"
echo "==========================================================="
echo
index=1
echo "Listing arg with \$*:"
for arg in $*
do
echo "Arg #$index=$arg"
let "index+=1"
done
echo "All the parameters is considered to be separated words"
echo "==========================================================="
echo
index=1
echo "Listing arg with \$@:"
for arg in $@
do
echo "Arg #$index=$arg"
let "index+=1"
done
echo "All the parameters is considered to be separated words"
echo "==========================================================="
echo
index=1
echo "Listing arg with \$#:"
for arg in $#
do
echo "Arg #$index=$arg"
let "index+=1"
done
echo "echo params"
[yangzheng@localhost shell_test]$
[yangzheng@localhost shell_test]$ sh test.sh 1 2 3 4
Listing args with"$*":
Arg #1=1 2 3 4
All the parameters is considered to be a word.
===========================================================
Listing arg with"$@":
Arg #1=1
Arg #2=2
Arg #3=3
Arg #4=4
All the parameters is considered to be separated words
===========================================================
Listing arg with $*:
Arg #1=1
Arg #2=2
Arg #3=3
Arg #4=4
All the parameters is considered to be separated words
===========================================================
Listing arg with $@:
Arg #1=1
Arg #2=2
Arg #3=3
Arg #4=4
All the parameters is considered to be separated words
===========================================================
Listing arg with $#:
Arg #1=4
echo params
[yangzheng@localhost shell_test]$ sh test.sh 1 “?”
[yangzheng@localhost shell_test]$ sh test.sh 1 "2 3" 4
Listing args with"$*":
Arg #1=1 2 3 4
All the parameters is considered to be a word.
===========================================================
Listing arg with"$@":
Arg #1=1
Arg #2=2 3
Arg #3=4
All the parameters is considered to be separated words
===========================================================
Listing arg with $*:
Arg #1=1
Arg #2=2
Arg #3=3
Arg #4=4
All the parameters is considered to be separated words
===========================================================
Listing arg with $@:
Arg #1=1
Arg #2=2
Arg #3=3
Arg #4=4
All the parameters is considered to be separated words
===========================================================
Listing arg with $#:
Arg #1=3
echo params
[yangzheng@localhost shell_test]$