目录

    如果大家有其他语言的基础或经验,就很快能明白数组了。简单来说,数组就某一种相同类型的元素组合,而后通过下标对其进行访问各元素。

  • 第一种形式
  1. array=(value1 value2 value3 ...)
  • 第二种形式
  1. array=([0]=value1 [1]=value2 [2]=value3 ...)

在形式中中括号代表的数组的下标索引,等号后面为其对应的值。

  • 第三种形式
  1. array[0]=value1;array[1]=value2;array[2]=value3
  • 第四种形式:动态数组
  1. array=($(命令))

  1. array=(`命令`)

在日常使用中推荐大家使用第一种形式和第中种形式

  • 第一种形式
  1. [root@localhost Test]# array=(1 3 5)
  2. [root@localhost Test]# echo ${array[*]}
  3. 1 3 5
  • 第二种形式
  1. [root@localhost Test]# array=([0]=1 [1]=3 [2]=5)
  2. [root@localhost Test]# echo ${array[*]}
  3. 1 3 5
  • 第三种形式
  1. [root@localhost Test]# array[0]=1;array[1]=3;array[2]=5
  2. [root@localhost Test]# echo ${array[*]}
  3. 1 3 5
  • 第四种形式:动态数组
  1. [root@localhost Test]# array=($(ls))
  2. [root@localhost Test]# echo ${array[*]}
  3. break.sh caseif.sh case.sh compareNum.sh

  1. [root@localhost Test]# array=(`ls`)
  2. [root@localhost Test]# echo ${array[*]}
  3. break.sh caseif.sh case.sh compareNum.sh

  数组输出通过采用下标索引的形式进行输出,输出数组的格式如下所示:

  1. ${ 数组名称 [下标索引] }

**如未指定数组下标,则默认下标索引从0开始;如果使用*或@则代表输出整个数组元素**。

  1. [root@localhost Test]# array=(1 3 5)
  2. [root@localhost Test]# echo ${array[2]} # 输出第3个元素
  3. 5
  4. [root@localhost Test]# echo ${array} # 未指定下标输出第1个元素
  5. 1
  6. [root@localhost Test]# echo ${array[*]} # 使用 * 输出整个数组元素
  7. 1 3 5
  8. [root@localhost Test]# echo ${array[@]} # 使用 @ 输出整个数组元素
  9. 1 3 5

  输出数长度常用格式如下所示:

  1. ${ #数组名称 [*] }
  2. ${ #数组名称 [@] }

示例如下所示:

  1. [root@localhost Test]# array=(1 3 5)
  2. [root@localhost Test]# echo ${#array[*]}
  3. 3
  4. [root@localhost Test]# echo ${#array[@]}
  5. 3

  可通过数组名[下标索引]对数组进行赋值,其格式如下所示:

  1. 数组名[下标索引]=value
  • 如果下标不存在,则自动向数组添加一个新的元素值
  • 如果下标存在,则会覆盖先前的元素值

示例如下所示:

  1. [root@localhost Test]# array=(1 3 5)
  2. [root@localhost Test]# array[2]=100 # 下标存在,覆盖之前的元素
  3. [root@localhost Test]# array[5]=888 # 下标不存在,则自动添加一个新的元素值
  4. [root@localhost Test]# echo ${array[*]}
  5. 1 3 100 888
  6. [root@localhost Test]# echo ${#array[@]}
  7. 4

  数组本质上还是一种变量,因此通过使用unset进行清除数组元素。其语法格式如下所示:

  1. unset 数组名称[下标索引]

如果不带下标索引,则表示清除整个数组,需要注意与输出数组元素不带下标索引的区别

示例如下所示:

  1. [root@localhost Test]# array=(1 3 5 7 9)
  2. [root@localhost Test]# echo ${array[@]}
  3. 1 3 5 7 9
  4. [root@localhost Test]# unset array[1] # 清除数组中第2个元素
  5. [root@localhost Test]# echo ${array[@]}
  6. 1 5 7 9
  7. [root@localhost Test]# unset array # 清除整个数组
  8. [root@localhost Test]# echo ${array[@]}
  9. # 清除数组后,输出为空
  1. [root@localhost Test]# b=(a b c d e f g h i)
  2. [root@localhost Test]# echo ${b[*]}
  3. a b c d e f g h i
  4. [root@localhost Test]# echo ${b[*]#a*} # 从左边开始匹配最短的数组元素并删除
  5. b c d e f g h i
  6. [root@localhost Test]# echo ${b[*]##b*} # 从左边开始匹配最长的数组元素并删除
  7. a c d e f g h i
  8. [root@localhost Test]# echo ${b[*]%i*} # 从右边开始匹配最短的数组元素并删除
  9. a b c d e f g h
  10. [root@localhost Test]# echo ${b[*]%%g*} # 从右边开始匹配最长的数组元素并删除
  11. a b c d e f h i
  12. [root@localhost Test]# echo ${b[*]} # 该删除并不影响原数组的内容
  13. a b c d e f g h i

数组从某种意义上来说,就是一种特殊的字符变量,因此也适合前面讲的子符串处理的功能。

  数组的截取示例如下所示:

  1. [root@localhost Test]# a=($(echo {0..9}))
  2. [root@localhost Test]# echo ${a[*]}
  3. 0 1 2 3 4 5 6 7 8 9
  4. [root@localhost Test]# echo ${a[*]:1:3} # 截取下标索引1~3的元素
  5. 1 2 3
  6. [root@localhost Test]# echo ${a[*]:0:2}# 截取下标索引0~2的元素
  7. 0 1

  数组的替换格式如下所示:

  1. ${ 数组名[*/@]/查找字符/替换字符 }

该替换操作并不会改变原先的数组内容

  数组的替换示例如下所示:

  1. [root@localhost Test]# echo ${a[*]}
  2. 0 1 2 3 4 5 6 7 8 9
  3. [root@localhost Test]# echo ${a[*]/4/456} # 将4替换为456
  4. 0 1 2 3 456 5 6 7 8 9
  5. [root@localhost Test]# echo ${a[*]}
  6. 0 1 2 3 4 5 6 7 8 9

1、使用循环打印数组元素

  1. [root@localhost Test]# cat array.sh
  2. #!/bin/bash
  3. array=($(echo {0..5}))
  4. echo "first method"
  5. for((i=0;i<${#array[*]};i++)) # 类C风格的for循环
  6. do
  7. echo ${i}
  8. done
  9. echo "second method"
  10. for ele in ${array[*]} # for in 循环
  11. do
  12. echo ${ele}
  13. done
  14. [root@localhost Test]# bash array.sh
  15. first method
  16. 0
  17. 1
  18. 2
  19. 3
  20. 4
  21. 5
  22. second method
  23. 0
  24. 1
  25. 2
  26. 3
  27. 4
  28. 5
  • 1、数组定义
  1. array=(1 2 3) # 静态数组
  2. array=($(ls)) # 动态数组
  • 2、数组赋值
  1. array[3]=5
  • 3、数组删除
  1. unset array[3]
  • 4、数组输出
  1. ${array[*]}或${array[@]} # 输出数组全部内容
  2. ${array[1]} # 输出数组单个元素
  • 5、数组长度
  1. ${#array[*]}或${#array[@]} # 输出数组长度
  • 6、循环输出数组元素
  1. for((i=0;i<${#array[*]};i++))
  2. do
  3. echo ${i}
  4. done
  5. for ele in ${array[*]}
  6. do
  7. echo ${ele}
  8. done

本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:
MyQRCode.jpg

版权声明:本文为surpassme原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/surpassme/p/10018237.html