Linux $* $@ 参数引用和数组元素引用


参数引用

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]# 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值