Linux 系统中 $* 和 $@的区别和联系
001、两者都可以表示shell脚本的所有参数,两者没有差异(不管是否增加双引号)
举例:
a、不加双引号
[root@PC1 test1]# ls ## 准备了两个测试脚本 a.sh b.sh [root@PC1 test1]# cat a.sh ## a.sh的内容如下 #!/bin/bash echo $* [root@PC1 test1]# cat b.sh ## b.sh的内容如下 #!/bin/bash echo $@ [root@PC1 test1]# bash a.sh one two three ## 执行a.sh,同时给脚本a.sh传递3个参数 one two three [root@PC1 test1]# bash b.sh one two three ## 执行b.sh,同时给脚本b.sh传递3个参数 one two three

b、增加双引号
[root@PC1 test1]# ls ## 两个测试脚本中都增加了双引号 a.sh b.sh [root@PC1 test1]# cat a.sh #!/bin/bash echo "$*" [root@PC1 test1]# cat b.sh #!/bin/bash echo "$@" [root@PC1 test1]# bash a.sh one two three ## 结果显示两者没有差异 one two three [root@PC1 test1]# bash b.sh one two three one two three

002、把$*和$@放入循环中,两者体现出差异(当使用双引号是两者有差异,不试用双引号时两者无差异)
a、不使用双引号
[root@PC1 test1]# ls ## 准备两个测试程序 a.sh b.sh [root@PC1 test1]# cat a.sh ## a.sh, $*不加双引号 #!/bin/bash for i in $* do echo $i done [root@PC1 test1]# cat b.sh ## b.sh, $@不加双引号 #!/bin/bash for i in $@ do echo $i done [root@PC1 test1]# bash a.sh one two three ## 两者无差异 one two three [root@PC1 test1]# bash b.sh one two three ## 两者无差异 one two three

b、使用双引号,两者出现差异
[root@PC1 test1]# ls a.sh b.sh [root@PC1 test1]# cat a.sh ## $*放入循环中,且使用双引号 #!/bin/bash for i in "$*" do echo $i done [root@PC1 test1]# cat b.sh ## $@放入循环中,且使用双引号 #!/bin/bash for i in "$@" do echo $i done [root@PC1 test1]# bash a.sh one two three ## 两者出现差异,其中 $*, 没有输出换行符, 也就是$i一次性输出了所有的参数, 是一个整体 one two three [root@PC1 test1]# bash b.sh one two three ## 两者出现差异, $@不是一个整体,仍然可以作为可迭代对象 one two three

003、两者出现的差异有何用途?
还不知道

浙公网安备 33010602011771号