目录
索引都是非负整数:五种形式
array=(value1 value2 value3 ...)
- eg:
- [root@localhost ~]# array1=(1 3 5 7 9)
- [root@localhost ~]# echo ${array1[*]}
- 1 3 5 7 9
array=([0]=one [1]=two [2]=three ... [n]=valuen)
- [root@localhost ~]# array2=([0]=1 [1]=3 [2]=5 [3]=7 [5]=9)
- #获取数组元素
- [root@localhost ~]# echo ${array2[*]}
- 1 3 5 7 9
- #获取数组下标
- [root@localhost ~]# echo ${!array2[*]}
- 0 1 2 3 5
array[num1]=value1;array[num2]=value2;array[num3]=value3
- [root@localhost ~]# array3[1]=22
- [root@localhost ~]# array3[3]=333
- [root@localhost ~]# array3[5]=555
- [root@localhost ~]# echo ${array3[*]}
- 1 22 333 555
- [root@localhost ~]# echo ${!array3[*]}
- 0 1 3 5
array=(命令)
- [root@localhost ~]# array4=(`seq 5`)
- [root@localhost ~]# echo ${!array4[*]}
- 0 1 2 3 4
- [root@localhost ~]# echo ${array4[*]}
- 1 2 3 4 5
关联数组:key值都是任意字符串
declare -A name
设置指定下标和对应变量
方法一:
name[num1]=value1
name[num2]=value2
name[num3]=value3
方法二:
name=([num1]=value1 [num2]=value2 [num3]=value3 ...)
- eg1:
- [root@localhost ~]# declare -A array5
- [root@localhost ~]# array5[a]=1
- [root@localhost ~]# array5[b]=a
- [root@localhost ~]# array5[c]=5
- [root@localhost ~]# echo ${array5[*]}
- 1 a 5
- [root@localhost ~]# echo ${!array5[*]}
- a b c
-
- eg2:
- [root@localhost ~]# array6=([a]=1 [b]=22 [c]=333)
- [root@localhost ~]# echo ${array6[*]}
- 1 22 333
- [root@localhost ~]# echo ${!array6[*]}
- a b c
# echo ${array[*]}
# echo ${array[@]}
- eg:
- [root@localhost ~]# array=(1 2 3 4 5)
- [root@localhost ~]# echo ${array[*]}
- 1 2 3 4 5
- [root@localhost ~]# echo ${array[@]}
- 1 2 3 4 5
# echo ${!array[*]}
# echo ${!array[@]}
- eg:
- [root@localhost ~]# echo ${!array[*]}
- 0 1 2 3 4
- [root@localhost ~]# echo ${!array[@]}
- 0 1 2 3 4
# echo ${#array[@]}
# echo ${#array[*]}
- eg:
- [root@localhost ~]# echo ${#array[@]}
- 5
- [root@localhost ~]# echo ${#array[*]}
- 5
#echo $[array[num]]
- eg:
- [root@localhost ~]# echo $[array[3]]
- 4
# array[num]=value
- eg:
- [root@localhost ~]# array[5]=6
- [root@localhost ~]# echo ${array[@]}
- 1 2 3 4 5 6
#array+=(value1 value2 value3 ...)
- eg:
- [root@localhost ~]# array+=(7 8 9)
- [root@localhost ~]# echo ${array[*]}
- 1 2 3 4 5 6 7 8 9
# unset array[num]
- eg:
- [root@localhost ~]# unset array[5]
- [root@localhost ~]# echo ${array[*]}
- 1 2 3 4 5 7 8 9
- [root@localhost ~]# echo ${!array[*]}
- 0 1 2 3 4 6 7 8
# unset array
- eg:
- [root@localhost ~]# unset array
- [root@localhost ~]# echo ${!array[*]}