0. understand the array
Subscript (1) shell does not limit the size of the array, the array elements counting from zero
(2) elements in the array to obtain the subscript [], can be an integer index, may be a result of the expression is an integer, but the index must be greater than or equal to 0
(3) bash shell supports only one-dimensional arrays, multidimensional arrays are not supported
1. Definition of an array
In the Shell, parentheses ( )
are denoted by separated by spaces between array, the array elements. Thus, the general form of an array is defined as:
array_name=(ele1 ele2 ele3 ... elen)
Note that an assignment =
on both sides No spaces must be next to the array name and array elements.
situation
|
script
|
Explanation
|
---|---|---|
Definition array |
|
echo ${nums[*]}----->29 100 13 8 91 44 |
Shell is weakly typed, it does not require the type of all array elements must be the same |
|
The third element is a "heterogeneous", the first two elements are integers, and the third element is a string. |
Shell length of the array is not fixed, then the element definition may also increase |
|
echo ${nums[*]}------>9 100 13 8 91 44 88 |
No need to assign to the array element by element, the following code is assigned only to specific elements: |
|
Only the above code assignment to the first element 3,5,10, so the length of the array is 3.
Noting that while the array length is 3, but the only real value of the array at the specified location, the value of other positions are empty, even though cross-border subscript position, its value is still empty, no error |
2. Get an array element
Get the value of array element, using the following general format: $ {array_name [index]}, where, ARRAY_NAME is the array name, index is the index.
situation
|
script
|
result
|
---|---|---|
Gets an array of the second element | echo ${nums[2]} | |
Use @ or * can get all the elements in the array |
echo ${nums[*]} echo ${nums[@]} |
|
For Li |
|
operation result: The output of all the array elements: 1357 Output of the second element: 5 Output of three elements: 7 The output of the four elements: The output of the five elements: 8 The output of all the array elements: 13578 |
3. Calculate the array length
Length of the array, i.e. the number of array elements can be used #
to obtain the number of array elements.
situation
|
script
|
result
|
---|---|---|
使用# 来获取数组元素的个数 |
${#array_name[@]} ${#array_name[*]} |
|
获取指定下标位置的元素长度 | 获取 arr 数组的第 2 个元素(假设它是字符串)的长度。 ${#arr[2]} |
|
字符串长度的获取方式 | ${#string_name} | |
举栗 |
|
运行结果: 输出数组的长度:4 字符串长度:10 输出数组的长度:5 第1个元素的长度:1 第8个元素的长度:10 输出数组的长度:4 |
4.拼接数组
所谓 Shell 数组拼接就是将两个数组连接成一个数组。
拼接数组的思路是:先利用@
或*
,将数组扩展成列表,然后再合并到一起。具体格式如下:
array_new=(${array1[@]} ${array2[@]})
array_new=(${array1[*]} ${array2[*]})
举栗
|
结果
|
---|---|
|
运行结果: 1 2 qwer tyui 1 2 qwer tyui |
5.删除数组元素或数组
情形
|
脚本
|
结果
|
---|---|---|
使用 unset 关键字来删除数组元素 | unset array_name[index] | |
删除整个数组 | unset array_name | |
举栗 |
|
运行结果: 23 99 66
注意最后的空行,它表示什么也没输出,因为数组被删除了,所以输出为空。 |
6.关联数组
shell中的关联数组类似于Python中字典的概念,关联数组也成为键值对数组,键(key)即数组的下标,值(value)即元素值。
6.1 创建关联数组
创建关联数组必须使用带有-A
选项的 declare 命令
情形
|
脚本
|
---|---|
新定义后赋值 |
|
定义时同时赋值 |
|
6.2 访问关联数组元素
情形
|
脚本
|
结果
|
---|---|---|
获取关联数组的元素值 | $(array_name["index"]) | echo ${people[num1]}-----> xiaohong |
获取所有元素的下标 | ${!array_name[@]} ${!array_name[*]} |
echo ${people[*]}----->xiaohong xiaobai xiaohei echo ${people[@]}----->xiaohong xiaobai xiaohei |
获取所有元素的值 | ${array_name[@]} ${array_name[*]} |
echo ${!people[*]}-----> num1 num2 num3 echo ${!people[@]}-----> num1 num2 num3 |
获取关联数组长度 与获取普通数组长度的方式相同 |
${#array_name[*]} |
echo ${#people[*]}-----> 3 echo ${#people[@]}-----> 3 |
举栗 |
|
运行结果:
|