Definition of 0x00 Array
The definition group of arrays has its place as a special data structure in any programming language, and of course the Bash shell is no exception. This article makes a small summary of the shell array. Here we only discuss the case of one-dimensional arrays, about multidimensional arrays (in fact, you have to use a one-dimensional array method to simulate), not involved. This includes copying, calculating, deleting, and replacing the array.
How arrays are defined:
1. Array[key]=value # arr[0]= "Hello" arr[1]= "World"
2. array= (value1 value2 ...) # arr= ("Hello" "world") note that there are spaces in front of parentheses
3. array= ([1]=value1 [2]=value2) # arr= ([0]= "Hello" [1]= "World")
Access to the 0x01 array
-Single access to elements in an array
${array[key]} # ${arr[1]}
-All elements in the array
Using the ${array name [subscript]} subscript is starting from 0 subscript is: * or @ Get the entire array contents
${array[* or @]} #${arr[*]}
-Calculate the length of the array
Use ${#数组名 [@ or *]} to get the array length
${#array [*]}
-Extraction of arrays
Directly through the ${array name [@ or *]: Start position: Length} Slice the original array, return is a string, the middle is separated by "space", so if you add "()", you will get the slice array
Arr= (1 2 3 4 5 6 7)
${arr[@]:0} # represents all elements
${ARR[@]:1} # Remove all elements after the first element
${arr[@]:0:2} # from 0 onwards fetch 2
-Deletion of array elements
Directly through: unset array [subscript] can clear the corresponding element, without subscript, clear the entire data.
Unset Arr[1]
-Substitution of array elements
${array name [@ or *]/find character/replace character} The operation does not change the contents of the original array
Arr= (1 2 3 4 5)
Echo ${ARR[@]/2/10} # result is 1 10 3 4 5
Arr= (${ARR[@]/2/10}) # Change the contents of the original array
Echo $arr # 1 10 3 4 5
Array of Shell programming