First, define the array variable
1. Grammar
(1) arrayname= (parameter1 parameter2 ...)
Example 1, define an array named name, with a value of Claire,obama,george
Name= (Claire Obama )
(2) Arrayname= (
Parameter1
Parameter2
...
)
Again we change example 1 to the second way
Name= (
Claire
Obama
George
)
Second, access to functions
(1) Subscript access
${ARRAYNAME[0]} Example 1 is Claire.
${ARRAYNAME[1]} Example 1 is Obama.
${ARRAYNAME[2]} Example 1 is George.
${arrayname[n]}
(2) @ and * access
${arrayname[@]} and ${arrayname[*]} get the elements in the array, example 1 is Claire Obama George
Third, get the number of arrays
(1) Get the number of the entire array
${#ArrayName [@]} and ${#ArrayName [*]} Example 1 is 3,
(2) Get the number of individual elements
${#ArrayName [0]} Example 1 is 5.
${#ArrayName [1]} Example 1 is 4.
${#ArrayName [2]} Example 1 is 5.
Iv. iterating through an array
(1) using while traversal
I=0
While [$i-lt ${#ArrayName [*]}]
Do
echo "parameter is: ${arrayname[$i]}"
((i++))
Done
(2) using for traversal
For parameter in ${arrayname[*]}
Do
echo "parameter is: $parameter"
Done
V. Emptying an array
(1) emptying a single array
arrayname[n]=
Note: When emptying a single array, the length of the array does not change
(2) Empty the entire array
1.unset Arrayname
2.arrayname=
Bash Shell array variables