Both $* and $@ variables provide fast access to all parameters, both of which can store all command-line arguments in a single variable.
The $* variable saves all parameters provided on the command line as a single word and is treated as a single argument instead of multiple objects.
The $@ variable treats all the arguments provided on the command line as multiple separate words in the agreed string. He allows you to iterate through all the values and separate each parameter provided. This is usually done through the for command.
Here is an example to see the ~
root@wl-ms-7673:/home/wl/Desktop/shell# cat-n test.sh
1 #!/bin/bash
2 echo "\$* and \$@ Test"
3 Echo ' \$* is: ' $* #这里两个输出结果是一样的
4 Echo ' \$@ is: ' $@ #
5 count=0
6 for var in ' $* '
7 do
8 count=$[$count +1]
9 echo "$count:" $var A echo "\$* done."
count=0 for var
in "$@" todo count=$[$count +1] echo "$count:" $var
" echo" \$@ done. "
root@wl-ms-7673:/home/wl/Desktop/shell#
The results of running the output are as follows:
root@wl-ms-7673:/home/wl/Desktop/shell#./test.sh a b c d E F
$* and $@ test
$* is:a b c d E F
$@ is:a b c D e f
1:a b c d E F
$* done.
1:a
2:b
3:c 4:d 5:e 6:f done
.
root@wl-ms-7673:/home/wl/Desktop/shell#