The array in the shell is the same as other programming languages, considering that the shell is a weak type of scripting language that can be defined and used directly in command lime
For example:
$a [1]= "Monday"
$echo ${a[1]}
Monday
$
Note that here with curly braces, if written in Echo $a [1], the shell expands before execution, the result is that a[1] is expanded to "a[1]" string, and then combined with $, directly output the string following the $, that is "a[1]", so when the array elements operate with curly braces, So as not to make unnecessary mistakes.
Assigning values to array elements
1 Assigning values individually
Simply give the specified array element a value, such as a[2]= "Tuesday"
2 Batch Assignment
form and other bad good, format is name= (value1 value2 value3 ...)
For example number= ("One", "" "" "" "" "" "Three" "four")
Of course you can specify subscript number= ([0]= "Zero" [1]= "one" [2]=] "[3]=" three ")
For example:
$number = ("One" "" "" "" Three ")
$echo ${number[0]}
One
$number 1= ([0]= "Zero" [1]= "one" [2]= ")" [3]= "three")
$echo ${number1[0]}
Zero
$
From here we know that the subscript of the array in the shell is starting from 0.
Operation of the array
1 size operation
The size of the array length can be extended by parameter extensions to understand the size of the array and the size of the array elements
For the example above, you can:
$echo ${#number [@]}//The size of the output array, which is the actual number of valid uses of the current array
3
$echo ${#number [2]}//Output the length of a third three
5
$
2 Find the subscript of the array, this feature in the previous language learning did not come into contact, it is based on the check array of one of the values, if any, the array is located in the subscript output, which can be found in the array of elements of great help,
Format is: ${!array[@]}
$number = ([1]= "Mon" [5]= "Fri")
$for i in "${!number[@]}"; do echo $i; Done
1
5
3 Append array elements at the end of the array, unlike other programming languages, where the specified array length cannot be changed, and in the shell it expands such features so that it can dynamically increase the length of the array as needed. But as you can see in the previous section of the operation array, you may find it difficult to know the maximum length of the array, and the shell provides a "+ =" operator to solve
$number = (1 2 3)
$echo ${number[@]}
1 2 3
$number + = (4 5 6)
$echo ${number[@]}
1 2 3 4 5 6
$
4 unset array can be deleted, the opposite is the definition of an array, declare-a array
You can also delete an element in an array
For example:
$number = (1 2 3)
$unset ' number[2] '//Specify the array element to be deleted here, and remember that you should use single quotes in order to prevent the shell from expanding.
$echo "${number[@]}"
1 2
$
5 The array name is actually the same as the address of the first element in the group, so your assignment to the name of the group is, in fact, the assignment of array[0]. But if the unset array is manipulated, the entire array is gone,
6 associative arrays, is the array subscript can be a string, this type of array expression meaning is very clear, in the use of the DECLARE-A array operation is the array array has such characteristics.
Shell array under Linux