$*和$@之间区别代码分析

#!/bin/bash
set 'apple pie' pears peaches
for i in $*           /*单引号被去掉,循环单个字符输出*/
do echo $i

done

[root@luozhonghua Ex_14.02-14.31]# sh 14-14-1
apple
pie
pears
peaches

--------------------------------------------------------------

#!/bin/bash
set 'apple pie' pears peaches
for i in "$*"      /*单引号被去掉,但当作字符串一次输出*/
do echo $i
done

[root@luozhonghua Ex_14.02-14.31]# sh 14-14-2
apple pie pears peaches
[root@luozhonghua Ex_14.02-14.31]#

-----------------------------------------------------------------

#!/bin/bash
set 'apple pie' pears peaches
for i in $@          /*单引号被去掉,循环单个字符输出*/
do echo $i     
done
[root@luozhonghua Ex_14.02-14.31]# sh 14-14-3
apple
pie
pears
peaches

-----------------------------------------------------------------

#!/bin/bash
set 'apple pie' pears peaches
for i in "$@"      /*每个位置参数都将当成一个加引号的字符串:"apple pie","pears ","peaches"*/
do echo $i
done
[root@luozhonghua Ex_14.02-14.31]# sh 14-14-4
apple pie
pears
peaches

时间: 2024-11-10 01:31:55