Array in shell
Array in shell
As a language, shell must not lack an array. The following describes the array content in shell.
Bash provides one-dimensional array variables. Any variable can be used as an array; the built-in command declare can explicitly define an array. There is no upper limit on the size of the array, and there is no limit on the requirements for continuous member reference and assignment. The array starts from 0 with an integer as the subscript.
1. Define and initialize Arrays
The following example illustrates how to define an array and how to initialize an array:
Declare-a array # The display declares the array arraydelcare-a array [10] # There is no upper limit on the array size, therefore, the specified size during definition will be ignored array [key] = value # array [0] = one, array [1] = twoarray = (value1 value2 ...) # The value format is [subscript] = string. The subscript and equal sign can be omitted, as shown in the following example. Array = (value1 value2 value3) # array [0] = value1, array [1] = value2, array [2] = value3array = ([0] = value1 [2] = value3 [3] = value [4])
From the above perspective, the definition of arrays is flexible and changeable, which can meet most of our needs. The biggest difference with other languages is that the array size in shell is not limited, it can also be understood that the array is dynamic.
2. Access to Arrays
Any element of the array can be used$ {Array [subscript]}To avoid conflicts with Path Extension.
If the subscript is@Or*, Which is extended to all members of the array. The two subscripts are only different in double quotation marks. In double quotation marks,$ {Name [*]}Extended as a word, which is composed of the values of all array members and separated by the first character of the IFS special variable;$ {Array [@]}Extends each Member of an array into a word. If the array has no members, $ {name [@]} is extended to an empty string.
#!/bin/basharr=(one two)for i in ${arr[*]}do echo ${i}done
The output result of the sample code above is as follows:
Lxg @ lxg-X240 :~ /Station/shell $ sh./array. sh
One two
#!/bin/basharr=(one two)for i in ${arr[@]}do echo ${i}done
The output result of the sample code above is as follows:
Lxg @ lxg-X240 :~ /Station/shell $ sh./array. sh
One
Two
3. delete an array
Use unset to delete an array, for example:
Unset array [2] # Delete the third member unset array # Delete the entire array
4. Length of the array
$ {# Arr [@] }$ {# arr [*] }$ {# arr} # error. The length of the first member of the array is obtained.
5. array "slicing" Operation
Used to obtain the "substring" of an array$ {Arr [@]: n: m}If no: MObtain the "string" from subscript n to the last element, for example:
#!/bin/bashecho ${arr[@]:2}echo ${arr[@]:1:3}echo ${arr[@]:0}
The output is as follows:
Three four
Two three four
Oner two three four
6. Associate an array
Shell can also declare an associated array. A common array can only use an integer as the index of the array, while an associated array uses a string as the index of the array. Is this correlated array a bit of dictionary in other languages... Haha
#!/bin/bashdeclare -A ass_arrass_arr[apple]=12ass_arr[orange]=19for t in ${ass_arr[@]}do echo ${t}doneecho ass_arr[orange]=${ass_arr[orange]}
The preceding sample output is as follows:
19
12
Ass_arr ["orange"] = 19
From the above output, the output sequence of the correlated array is somewhat different from that of the ordinary array. The correlated array is output starting from the last member.
The associated array uses a string as the index. Sometimes we need to obtain all the indexes of the array, which can be obtained in the following way:
#!/bin/bashdeclare -A ass_arrass_arr[apple]=12ass_arr[orange]=19echo ${!ass_arr[@]} #or ${!ass_arr[*]}
The output is as follows:
Apple orange