Variable: Stores the memory space of a single element:
Array (multiple contiguous memory spaces): a contiguous memory space that stores multiple elements:
Array Name:
Index: Numbering starting from 0, is a numeric index;
Note: Indexes can also support the use of custom formats, not just numeric formats:
The bash array supports sparse formatting
Referencing an array of elements: ${array_name[index]}
Declaring an array:
Declare-a Array_Name
Declare-a array_name: Associative array: KV
Assignment of array elements:
(1) Assign only one element at a time:
Array_name[index]=value
#weekdays [0]= "Sunday"
#weekdays [4]= "Thursday"
(2) Assign all elements at once:
Array_name= ("VAL1", "VAL2", "VAL3",...)
(3) Assign only specific elements:
Array_name= ([0]= "VAL1" [3]= "VAL2" ...)
(4) Read-a ARRAY
To reference an element in an array:
${array_name[index]}
Note: Omitting [INDEX] means referencing an element with subscript 0
Displays all elements in the array:
${array_name[*]}, or ${array_name[@]}
The length of the statistic array (the number of elements in the array):
${#ARRAY_NAME [*]},${#ARRAY_NAME [@]}
To reference an element in an array:
Remove all elements in the array in turn: ${array[@]} or ${array[*]}
Array slices:
Remove an element of the specified length in the array: ${array[@]:offset:number}: Also known as array slicing
Offset: Number of elements to skip
Number: How many elements to remove
Example: #echo ${weekdays[@]:1:2}
Remove all elements after offset: ${array[@]:offset}
#echo ${weekdays[@]:1}
Append an element to the array:
array[${#ARRAY [*]}]
To delete an element in an array:
Unset Array[index]
Associative arrays:
Declare-a Array_Name
Array_name= ([index_name]= ' val1 ' [index_name2= ' val2 ' ...])
This article is from the "burning Years of Passion" blog, please be sure to keep this source http://liuzhengwei521.blog.51cto.com/4855442/1927663
Usage of shell arrays