The array definition in the shell:
You can define an array as a whole:
Array_name= (value0 value1 value2 value3 ...)
Or:
Array_name= (Value0value1value2value3 ...)
At this point the array's subscript defaults to 0, and the individual components of the array can be defined individually:
Array_name[0]=value0array_name[1]=value1array_name[n]=valuen ...
You can not use successive subscripts, and there is no limit to the range of subscripts.
Gets the elements in the array:
Valuen=${array_name[n]}
All the elements in the array are worth at once:
Echo ${array_name[@]}
gets the number of array elements :
length=${#ARRAY_NAME [@]} or length=${#ARRAY_NAME [*]}
gets the length of an array of individual elements :
lengthn=${#ARRAY_NAME [n]}
Array Traversal:
To iterate through an array using the while loop:
i=0length=${#ARRAY_NAME [*]}while [$i-lt $length]; dovaluei=${array_name[$i]}...let I++done
To iterate through an array with a For loop:
For value in ${array_name[*]}; Dovaluei=value...done
Empty the array:
To empty a single element:
array_name[n]=
Empty the entire array:
Unset array_name or: array_name=
It should be noted that the variable after unset should not be added $. The plus $ will empty the variable with the Array_Name value as the name.
Linux array variables