Shell array variable
Array definition in shell:
You can define the array as a whole:
ARRAY_NAME= (value0 value1 value2 value3...)
Or:
ARRAY_NAME= (value0value1value2value3...)
At this point, the subscript of the array starts at 0 by default, and each component of the array can be defined separately:
ARRAY_NAME [0] = value0ARRAY_NAME [1] = value1ARRAY_ name [n] = valuen...
There can be no use of consecutive subscripts, and there is no limit to the range of subscripts.
Get the elements in the array:
Valuen=$ {ARRAY_ NAME[n]}
All the elements in the array are worth it at once:
Echo ${ARRAY_NAME [@]}
Get the number of elements in the array:
Length=$ {# ARRAY_NAME [@]} or length=$ {# ARRAY_NAME [*]}
Gets the length of a single element of the array:
Lengthn=$ {# ARRAY_ NAME[n]}
Array traversal:
Use while to loop through the array:
While [$I-lt $length]; dovaluei=$ {ARRAY_NAME [$]}. Let i++done
Use for to loop through the array:
For value in ${ARRAY_NAME [*]}; dovaluei=value...done
Empty the array:
Empty a single element:
ARRAY_ name [n] =
Empty the entire array:
Unset ARRAY_NAME or: ARRAY_NAME=
It should be noted that variables after unset should not add $. Adding $clears the variable with the value of ARRAY_NAME.