Defining arrays
A= (1 2 3 4 5 6)
Print array
echo ${a[@]} or echo ${a[*]}
[[email protected] shell]# a=(1 2 3 4 5 6)[[email protected] shell]# echo ${a[@]}1 2 3 4 5 6[[email protected] shell]# echo ${a[*]}1 2 3 4 5 6
Print the elements in an array by subscript
Gets the number of array elements
echo ${#a [@]}
[[email protected] shell]# echo ${#a[@]}6
The assignment of an array exists, then the substitution does not exist, and the "position by subscript" is added.
[[email protected] shell]# a[6]=100[[email protected] shell]# echo ${a[@]}1 2 3 4 5 6 100[[email protected] shell]# a[6]=aa[[email protected] shell]# echo ${a[@]}1 2 3 4 5 6 aa
Array Delete element "position by subscript"
Slices of an array
[[email protected] shell]# b=(`seq 1 10`)[[email protected] shell]# echo ${b[@]}1 2 3 4 5 6 7 8 9 10[[email protected] shell]# echo ${b[@]:3:4} #从下标为三的元素开始截取 截取4个4 5 6 7[[email protected] shell]# echo ${b[@]:0-3:2} #从下标为倒数第三的元素开始截取 截取2个8 9
Substitution of array elements
[[email protected] shell]# echo ${b[@]}1 2 3 4 5 6 7 8 9 10[[email protected] shell]# echo ${b[@]/3/33} #只在显示结果里替换元素 数组不变1 2 33 4 5 6 7 8 9 10[[email protected] shell]# echo ${b[@]/7/77} #只在显示结果里替换元素 数组不变1 2 3 4 5 6 77 8 9 10[[email protected] shell]# echo ${b[@]}1 2 3 4 5 6 7 8 9 10
[[email protected] shell]# b=(${b[@]/8/888}) #改变数组组成元素[[email protected] shell]# echo ${b[@]}1 2 3 4 5 6 7 888 9 10
Arrays in the shell