There are two types of arrays in bash: normal arrays and associative arrays. An ordinary array can use integer values only as an array index, and associative arrays may use strings as indexes.
1.7.1 Normal Array
Define an array in one way:
[Email protected] tmp]# array_test= (1 2 3 4)
They are stored in the position of index bit 0-3, respectively, array_test[0] to array_test[3] corresponding values.
[[email protected "tmp ]# echo ${array_test[2]} ç How arrays are referenced Span lang= "en-US" >:${array_name[index]}
3
Note that the definition in the array is defined in parentheses using spaces as delimiters instead of commas. If you use commas, they will act as a whole, which is the value of array index 0.
If you use commas, then:
[email protected] tmp]# array_test= (1,2,3,4)
[[email protected "tmp ]# echo ${array_test[0]} ç overall result as index bit.
1,2,3,4
How to define an array two: You can customize the index bit.
[Email protected] tmp]# array_test[1]=1
[Email protected] tmp]# array_test[2]=2
[Email protected] tmp]# array_test[3]=3
[Email protected] tmp]# array_test[4]=4
[[email protected] tmp]# echo ${array_test[*]}
1,2,3,4 1 2 3 4
Prints all the values of the array.
[[email protected] tmp]# echo ${array_test[*]}
1,2,3,4 1 2 3 4
Or:
[[email protected] tmp]# echo ${array_test[@]}
1,2,3,4 1 2 3 4
View the array index number.
[[email protected] tmp]# echo ${!array_test[*]}
0 1 2) 3 4
Or
[[email protected] tmp]# echo ${!array_test[@]}
0 1 2) 3 4
1.7.2 Statistic Array length
[[email protected] tmp]# echo ${#array_test [*]}
5
[[email protected] tmp]# echo ${#array_test [@]}
5
1.7.3 Associative arrays
Associative arrays support strings as an array index. Using an associative array must first declare it with DECLARE-A.
[[email protected "tmp ]# declare-a array_dep ç after declaration, it can be assigned a value
[Email protected] tmp]# array_dep= ([Name1]=longshuai [Name2]=xiaofang]
[[email protected] tmp]# echo ${array_dep[name1]}
Longshuai
You can also assign values separately.
[Email protected] tmp]# Array_dep[name3]=zhangsan
[Email protected] tmp]# Array_dep[name4]=lisi
[[email protected] tmp]# echo ${array_dep[name4]}
Lisi
View all values of the array.
[[email protected] tmp]# echo ${array_dep[*]}
Zhangsan Xiaofang Longshuai Lisi C you can see the letters in reverse order.
Or:
[[email Protected] tmp]# echo ${array_dep[@]}
Zhangsan Xiaofang Longshuai Lisi C you can see the letters in reverse order.
View the array index number.
[[email protected "tmp ]# echo ${!array_dep[@]} ç reverse alphabetical order
Name3 name2 name1 Name4
Or:
[[email protected] tmp]# echo ${!array_dep[*]}
Name3 name2 name1 Name4
The statistic array length.
[[email protected] tmp]# echo ${#array_dep [*]}
4
Or:
[[email protected] tmp]# echo ${#array_dep [@]}
4
Shell Script Raiders (learning notes)--1.7 array