(1) Definition of an array
[Email protected]:~# a= (1 2 3 4)
[Email protected]:~# echo ${a[1]}
2
[Email protected]:~# a[0]=1
[Email protected]:~# a[1]=2
[Email protected]:~# echo ${a[0]}
1
(2) Array worth acquiring
[Email protected]:~# var[0]=1
[Email protected]:~# var[1]=2
[Email protected]:~# echo $var [1]
1[1]
[Email protected]:~# echo ${var[1]}
2
(3) Gets the length of the array. use ${#数组名 [@ or *]} to get the array length
[Email protected]:~# a= (10 20 30 40 50 60)
[Email protected]:~# echo ${#a [*]}
6
[Email protected]:~# echo ${#a [@]}
6
[Email protected]:~# echo ${a[@]}
10 20 30 40 50 60
[Email protected]:~# echo ${a[*]}
10 20 30 40 50 60
(4) array read, with ${array name [subscript]} subscript is starting from 0 subscript is: * or @ Get the entire array contents
[Email protected]:~# echo ${a[1]}
20
[Email protected]:~# echo ${a[*]}
10 20 30 40 50 60
(5) Assigning values to arrays
[Email protected]:~# a[3]=100
[Email protected]:~# echo ${a[*]}
10 20 30 100 50 60
(6) The deletion of the array, directly through: unset array [subscript] can clear the corresponding element, without subscript, clear the entire data.
[Email protected]:~# echo ${a[*]}
10 20 30 100 50 60
[Email protected]:~# unset a[1]
[Email protected]:~# echo ${#a [@]}
5
[Email protected]:~# echo ${a[*]}
[Email protected]:~# unset a
[Email protected]:~# echo ${#a [*]}
0
(7) Shard use, directly through the ${array name [@ or *]: Start position: Length} Slice the original array, return is a string, the middle with "space" separate, so if you add "()", will get the slice array, the above example: C is a new data.
A:TT is just a variable, not an array, so be sure to add it ()
[Email protected]:~# Tt=${a[*]:2:4}
[Email protected]:~# Echo $TT 30 40 50 60
[Email protected]:~# echo ${tt[1]}
[Email protected]:~# echo ${tt[2]}
B:C is still an array
[Email protected]:~# echo ${a[*]}
10 20 30 40 50 60
[Email protected]:~# c= (${a[*]:1:4})
[Email protected]:~# echo ${c[*]}
20 30 40 50
[Email protected]:~# Echo $c
20
[Email protected]:~# echo ${c[2]}
40
[Email protected]:~# echo ${a[*]}
10 20 30 40 50 60
(8) Replace, The Call method is: ${array name [@ or *]/find character/substitution character} This operation will not change the original array contents, if you need to modify, you can see the above example, redefine the data.
[Email protected]:~# echo ${a[*]}
10 20 30 40 50 60
[Email protected]:~# Echo ${a[*]/60/100}
10 20 30 40 50 100
[Email protected]:~# echo ${a[*]} //The value of array A is constant
10 20 30 40 50 60
[Email protected]:~# tt=${a[*]/100/1000} //Without parentheses, it is a variable, not an array
[Email protected]:~# Echo $tt
10 20 30 40 50 60
[Email protected]:~# echo ${tt[1]}
[Email protected]:~# tt= (${a[*]/60/1000}) //with parentheses, then TT is an array
[Email protected]:~# echo ${tt[5]}
1000
[Email protected]:~# echo ${tt[*]}
10 20 30 40 50 1000
[Email protected]:~# echo ${a[*]} //Array A is unchanged
10 20 30 40 50 60
A one-dimensional array in the shell is worth getting