Array as a data structure, in some high-level languages are directly provided and implemented, of course, my shell is also some ha.
PS: This article is written because brother B in the group sent a screenshot:
My big B elder brother is still omnipotent talent ha ... Cut the crap and talk about it.
1, how to declare an array variable?
Direct assignment:
Copy Code code as follows:
array[0]= "Zero" array[1]= "one" array[2]= "two"
Declare statement:
Copy Code code as follows:
Parenthesis Space Method:
Copy Code code as follows:
Quotation mark Space Method:
Copy Code code as follows:
Mixing method:
Copy Code code as follows:
array= ([0]= "Zero" [1]= "one" [2]= "two")
2, how to traverse an array?
Standard for loop:
Copy Code code as follows:
[Chenqing@qing ~] for ((i=0;i<${#array [@]};i++)) do echo ${array[i]}; Done
Zero
One
Two
For...in:
Copy Code code as follows:
[chenqing@qing ~] for i in ${array[@]};d o echo $i;d one
Zero
One
Two
3, get the length of the array
Copy Code code as follows:
[chenqing@qing ~] echo ${#array [@]}
3
[chenqing@qing ~] echo ${#array [*]}
3
4. Delete array elements
Copy Code code as follows:
[chenqing@qing ~] unset array[0]
[chenqing@qing ~] echo ${#array [@]}
2
[chenqing@qing ~] for i in ${array[@]};d o echo $i;d one
One
Two
5, delete the array
Copy Code code as follows:
6, like the Python slice
Copy Code code as follows:
[chenqing@qing ~] Echo ${array[@]:0:1}
Zero
[chenqing@qing ~] Echo ${array[@]:0:2}
Zero One
[chenqing@qing ~] Echo ${array[@]:0:3}
Zero One Two
[chenqing@qing ~] Echo ${array[@]:1}
One Two
[chenqing@qing ~] Echo ${array[@]:2}
Two