[20210913]bash shell $* and $@ 的区别.txt

[20210913]bash shell $* and $@ 的区别.txt

--//bash shell $* and $@ 都是输出bash shell的整个命令行参数的内容,两者还是有点区别的,通过例子说明:

$ cat disp_argv_params.sh
#!/bin/bash
# display all the parameters
#
echo
echo "Using the \$* method: $*"
echo "$*" | xargs -IQ echo Q

count=1
for param in "$*"
do
     echo "\$* Parameter #$count = $param"
     count=$[ $count + 1 ]
done

echo
echo "Using the \$@ method: $@"
echo "$@" | xargs -IQ echo Q

count=1
for param in "$@"
do
     echo "\$@ Parameter #$count = $param"
     count=$[ $count + 1 ]
done
echo

--//测试:
$ . disp_argv_params.sh 1111 2222 3333        4444

Using the $* method: 1111 2222 3333 4444
1111 2222 3333 4444
$* Parameter #1 = 1111 2222 3333 4444

Using the $@ method: 1111 2222 3333 4444
1111 2222 3333 4444
$@ Parameter #1 = 1111
$@ Parameter #2 = 2222
$@ Parameter #3 = 3333
$@ Parameter #4 = 4444
--//咋一看似乎感觉差不多,不过你仔细看for就明白了.

$ . disp_argv_params.sh 1111 2222 3333                 "  4444"

Using the $* method: 1111 2222 3333   4444
1111 2222 3333   4444
$* Parameter #1 = 1111 2222 3333   4444

Using the $@ method: 1111 2222 3333   4444
1111 2222 3333   4444
$@ Parameter #1 = 1111
$@ Parameter #2 = 2222
$@ Parameter #3 = 3333
$@ Parameter #4 =   4444

请使用浏览器的分享功能分享到微信等