1. array definition
$ a=(1 2 3 4 5 6) $ echo $a
1
Note: array elements separated by spaces, no spaces = both sides, echo $ a only returns the first element of the array
2. Array reading assignment
2.1 Gets an array of length
$ Echo $ {# a [@]} 6
With the array name $ {# [@ or *]} can be an array of length
2.2 Gets an array of elements
$ Echo $ {a [ 0 ]} 1
$ Echo $ {a [*]}
1 2 3 4 5 6
Note: subscript is * or @ retrieves all of the elements
2.3 Assignment
$ A [ 1 ] = 11 $ a [ 3 ] = 33 $ Echo $ {a [@]} 1 11 2 33 4 5 6
Note: If the index does not exist, automatically add a new array element,
2.4 Removing elements
$ a=(1 2 3 4 5) $ unset a $ echo ${a[*]} $ a=(1 2 3 4 5) $ unset a[1] $ echo ${a[*]} 1 3 4 5 $ echo ${#a[*]} 4
Directly through: unset array [index] to clear the corresponding elements, not subscripts, clears the entire data.
3. Special Use
Fragment 3.1
$ A = ( 1 2 3 4 5 ) $ Echo $ {a [*]: 1 : 3 } 2 3 4 $ b = ($ {a [*]: 2 : 9 }) $ Echo $ {b [* ] } 3 4 5
$ c = $ {a [*]: 2: 9}
$ miss $ c
3 4 5
Directly through the array name $ {[@ or *]: Start position: length} slices original list, returns a string, an intermediate "space" is separated, if so with "()", and the resulting array slice, the above example : b is a new array, and c is a string
Note: If the specified length is greater than the array itself, to take only the last element in the array, the error will not
Replace 3.2
$ A = ( 1 2 3 4 5 ) $ Echo $ {a [*] / 3 / 33 } 1 2 33 4 5 $ Echo $ {a [* ]} 1 2 3 4 5 $ a = ($ {a [ *] / 3 / 33 }) $ Echo $ {a [* ]} 1 2 33 4 5
Call the method is: $ {array name [@ or *] / characters find / replace characters} This operation does not alter the original contents of the array
Note: Replacement of a more fan operation
$ a=(12 222 345) $ echo ${a[*]/2/88} 188 8822 345 $ b=(${a[*]/2/88}) $ echo {b[*]} 188 8822 345 $ echo ${b[*]/2/88} 188 88882 345
Where each element in the lookup are then again replaced, and replacement only once, such as a [2] 222, it becomes only replaces the previous 8822,