参数引用
1. $*=$@="$@“ 其作用相同,使用for循环可以按空格为界读取每一个参数
2. "$*" 是将所有参数看成一个序列,使用for读取时,指读取一个元素
数组元素引用
1. ${ar[*]}=${ar[@]}=”${ar[@]}“ 其作用相同,使用for循环可以读取每一个数组元素
2. “${ar[*]}” 是将所有数组元素看成一个序列,使用for读取整个数组序列,只包含一个元素
关联数组索引引用
1. ${!ar[*]}=${ar[!@]}=”${!ar[@]}“ 其作用相同,使用for循环可以读取每一个数组元素的索引值
2. “${!ar[*]}” 是将所有数组元素的索引值看成一个序列,使用for读取整个序列,只包含一个元素
详见如下代码:
#!/bin/bash
echo "--------------------"
echo 'for i in $@'
for i in $@
do
echo $i
done
echo "--------------------"
echo 'for i in "$@"'
for i in $@
do
echo $i
done
echo "--------------------"
echo 'for i in $*'
for i in $*
do
echo $i
done
echo "--------------------"
echo 'for i in "$*"'
for i in "$*"
do
echo $i
done
declare -A ar
ar[hi]=1
ar[fuck]=2
ar[dd]=3
echo '${ar[*]}'
echo ${ar[*]}
echo '${ar[@]}'
echo ${ar[@]}
echo '${!ar[*]}'
echo ${!ar[*]}
echo '${!ar[@]}'
echo ${!ar[@]}
echo "--------------------"
echo 'for i in "${ar[@]}" '
for i in "${ar[@]}"
do
echo $i
done
echo "--------------------"
echo 'for i in ${ar[@]} '
for i in ${ar[@]}
do
echo $i
done
echo "--------------------"
echo 'for i in "${ar[*]}" '
for i in "${ar[*]}"
do
echo $i
done
echo "--------------------"
echo 'for i in ${ar[*]} '
for i in ${ar[*]}
do
echo $i
done
echo "--------------------"
echo 'for i in "${!ar[@]}" '
for i in "${!ar[@]}"
do
echo $i
done
echo "--------------------"
echo 'for i in ${!ar[@]} '
for i in ${!ar[@]}
do
echo $i
done
echo "--------------------"
echo 'for i in "${!ar[*]}" '
for i in "${!ar[*]}"
do
echo $i
done
echo "--------------------"
echo 'for i in ${!ar[*]} '
for i in ${!ar[*]}
do
echo $i
done
运行结果:
[root@localhost myshell]# t.sh 1 2 3
--------------------
for i in $@
1
2
3
--------------------
for i in "$@"
1
2
3
--------------------
for i in $*
1
2
3
--------------------
for i in "$*"
1 2 3
${ar[*]}
3 1 2
${ar[@]}
3 1 2
${!ar[*]}
dd hi fuck
${!ar[@]}
dd hi fuck
--------------------
for i in "${ar[@]}"
3
1
2
--------------------
for i in ${ar[@]}
3
1
2
--------------------
for i in "${ar[*]}"
3 1 2
--------------------
for i in ${ar[*]}
3
1
2
--------------------
for i in "${!ar[@]}"
dd
hi
fuck
--------------------
for i in ${!ar[@]}
dd
hi
fuck
--------------------
for i in "${!ar[*]}"
dd hi fuck
--------------------
for i in ${!ar[*]}
dd
hi
fuck
[root@localhost myshell]#