linux 中 数组的常见操作

发布时间 2023-07-05 18:20:07作者: 小鲨鱼2018

 

001、创建数组

[root@PC1 test02]# ay=(1 2 3 4)         ## 生成数组
[root@PC1 test02]# echo ${ay[*]}        ## 输出数组
1 2 3 4
[root@PC1 test02]# echo ${#ay[*]}       ## 输出数组的长度
4

 

002、

[root@PC1 test02]# ay=("a", "b", "c", "x")    ## 字符串数组
[root@PC1 test02]# echo ${ay[*]}             
a, b, c, x
[root@PC1 test02]# echo ${ay[@]}              ## 输出数组
a, b, c, x
[root@PC1 test02]# echo ${#ay[*]}             ## 输出数组的长度
4

 

003、输出单个数组元素的值

[root@PC1 test02]# ay=("a", "b", "c", "x")         ## 生成数组
[root@PC1 test02]# echo ${ay[0]}                   ## 输出单个元素的值
a,
[root@PC1 test02]# echo ${ay[2]}
c,
[root@PC1 test02]# for i in {0..3}; do echo ${ay[$i]}; done      ## 遍历
a,
b,
c,
x

 。