Shell学习——数组

1、普通数组:只能用整数作为索引
1.1、赋值
[root@client02 ~]# array[0]=test1
[root@client02 ~]# array[1]=test2
[root@client02 ~]# array[2]=test3
1.2、打印
[root@client02 ~]# echo ${array[0]}
test1
[root@client02 ~]#
[root@client02 ~]# echo ${array[*]}
test1 test2 test3
[root@client02 ~]# echo ${array[@]}
test1 test2 test3
1.3、打印数组长度
[root@client02 ~]# echo ${#array[*]}
3
[root@client02 ~]#
1.4、列出数组索引
[root@client02 ~]# echo ${!array[*]}
0 1 2
[root@client02 ~]#
2、关联数组:可以使用任何文本作为索引
2.1、关联数组定义
[root@client02 ~]# declare -A fruits_value
[root@client02 ~]# fruits_value=([apple]='10 yuan' [orange]='8 yuan')
[root@client02 ~]# echo "Apple costs ${fruits_value[apple]}"
Apple costs 10 yuan
[root@client02 ~]# echo ${!fruits_value[*]}
orange apple
[root@client02 ~]#

原文地址:https://www.cnblogs.com/pigwan7/p/9629311.html