关于在shell脚本中数组变量中 “*”跟 “@” 区别
“*”
当变量加上“”
会当成一串字符串处理.
“@”
变量加上“”
依然当做数组处理.
在没有加上“” 的情况下 效果是等效的.
#!/bin/bash
array=(1 2 3)
echo "case 1"
for line in "${array[@]}"
do
echo $line
done
echo "case 2"
for line in "${array[*]}"
do
echo $line
done
echo "case 3"
for line in ${array[*]}
do
echo $line
done
echo "case 4"
for line in ${array[@]}
do
echo $line
done
输出结果如下:
case 1
1
2
3
case 2
1 2 3
case 3
1
2
3
case 4
1
2
3
As mentioned in man bash:
If the word is double-quoted, ${name[*]}
expands to a single word with the value of each array member separated by the first character of the IFS special variable, and ${name[@]}
expands each element of name to a separate word.
Examples:
array=("1" "2" "3")
printf "'%s'" "${array[*]}"
'1 2 3'
printf "'%s'" "${array[@]}"
'1''2''3'
注意:如果使用"${array[@]}"
作为参数传递的话,就只会传入第一个参数
原文链接:https://blog.csdn.net/weixin_30755709/article/details/101181684