Array
An Introduction to arrays
The definition of the usual a=1,b=2,c=3; if there are many variables, one more definition is laborious, and the variables are laborious.
Simply put, an array is a collection of elements of the same data type sorted in a certain order
An array is named after a variable of the same type with a different number, and then the set of variables is distinguished by its name, which is the array name, which becomes the subscript of the array, and the variables that make up the array are the components of the array, also known as the elements of the array.
With arrays, you can refer to a series of variables with the same name and identify them with arrays (indexes), and in many cases, using arrays can shorten and simplify the development of your program, because you can use indexed values to design a loop to efficiently handle multiple situations
Definition and additions and deletions of arrays
1, the definition of the array
Method 1:array_name= (value0 value1 value2 value3)
Method 2:
Array_name= (
Value0
Value1
value2
Value3
)
Method 3:
Array_name[0]=value0
Array_name[1]=value1
Array_name[2]=value2
2. Get the length of the array
[Email protected] ~]# arr= (1 2 3 4)
[[email protected] ~]# echo ${#arr [@]}
4
[[email protected] ~]# echo ${#arr [*]}
4
3. Get the value of an array element
${array_name[@]} value of all elements
${array_name[*]} value of all elements
${array_name[i]} The value of a single element, I is subscript
[[email protected] ~]# echo ${arr[0]}
1
[[email protected] ~]# echo ${arr[1]}
2
[[email protected] ~]# echo ${arr[2]}
3
[[email protected] ~]# echo ${arr[3]}
4
[[email protected] ~]# echo ${arr[@]}
1 2 3 4
[[email protected] ~]# echo ${arr[*]}
1 2 3 4
[Email protected] ~]#
[[email protected] ~]# for ((i=0;i<${#arr [*]};i++));d o echo ${arr[$i]};d One
1
2
3
4
[Email protected] ~]#
==============
Read the values of the array individually
For item in ${myarray[*]};
Do
Echo $item;
Done
[Email protected] ~]# arr= (1 2 3 4)
[[email protected] ~]# for n in ${arr[@]};d o echo $n;d one
1
2
3
4
[Email protected] ~]#
4, array increment element, array assignment can
# # #array_name [N]=value
[[email protected] ~]# for n in ' seq 0 1 3 ';d o echo ${arr[$n]};d One
1
2
3
4
[[email protected] ~]# echo ${#arr [@]}
4
[Email protected] ~]# arr[4]=5
[[email protected] ~]# echo ${#arr [@]}
5
[[email protected] ~]# for n in ' seq 0 1 4 ';d o echo ${arr[$n]};d One
1
2
3
4
5
5. Array deletion
Delete an element of an array: unset array_name[n]
OR: ${array_name[@ or *] #字符} from the left to match delete shortest
${array_name[@ or *]# #字符} from the left to match delete the longest
${array_name[@ or *]% characters} from right start match Delete shortest
${array_name[@ or *]%% characters} from right start match delete longest
Delete entire array: unset array_name
[[email protected] ~]# echo ${arr[4]}
5
[Email protected] ~]# unset arr[4]
[[email protected] ~]# echo ${arr[4]}
[Email protected] ~]#
############################
[Email protected] ~]# unset arr
[[email protected] ~]# echo ${arr[3]}
[[email protected] ~]# echo ${#arr [@]}
0
[Email protected] ~]#
6. Interception and substitution of arrays (much like the substitution of variable quantum strings)
Intercept
[[email protected] ~]# arr= (a b c D E F g)
[[email protected] ~]# echo ${arr[@]:1:3} ##${arr[@]: Start of interception: intercept length}
b c D
[[email protected] ~]# echo ${arr[@]:3:2}
D E
[Email protected] ~]#
Replace
[[email protected] ~]# arr= (a b c D E F g)
[[email protected] ~]# echo ${arr[@]/a/xiaogao}
Xiaogao b c d E F g
[[email protected] ~]# echo ${arr[0]}
A
[Email protected] ~]#
Call method: ${array name [@ or *]/match character/replacement character}, the operation does not change the contents of the principle array, if need to modify, it needs to be redefined
This article is from the "Struggle Bar" blog, please be sure to keep this source http://lvnian.blog.51cto.com/7155281/1701546
Shell Array Explanation