Bash Two types of arrays are provided , Index array (indexed array) And associative arrays (associative array). This paper mainly introduces the basic usage of index array .

Basic characteristics of index array

Bash The provided arrays are all one-dimensional arrays .
Any variable can be used as an indexed array .
adopt declare Keyword can explicitly declare an index array .
The size of an array is unlimited .
The index of an element of an indexed array does not have to be continuous .
Array elements are referenced by zero based integers .
Unless otherwise noted , Index arrays must use non negative integer index elements .

Create index array automatically

The index array can be implicitly created by the assignment statement :
$ myArr[0]=apple
By indexing 0 Element assignment for , variable myArr Will be initialized to an index array .
Of course it can be :
$ myArr[3]=orange
No need to index 0 Start assignment .

Explicitly declare an index array

adopt declare Keywords and -a Option to explicitly declare an indexed array :
$ declare -a myArr
Then initialize :
$ myArr=(apple orange pear)
It can also be initialized at the time of declaration :
$ declare -a myArr=(apple orange pear)
Or initialize only one element after declaration :
$ myArr[0]=grape
Reference array element

Referencing array elements is a little more cumbersome , Not only do you want to specify the index location of the element , You also need to use ${} Symbol . Please note that , Index array subscript from 0
start , And cannot be negative ( I wish the array elements in all programming languages 0 Start index !).
$ myArr[0]=apple $ echo ${myArr[0]}


If you accidentally reference an element without an assignment ? never mind , There's no crash here , It's just that there's no output :



We can also easily output all elements of the array :
$ declare -a myArr=(apple orange pear) $ myArr[10]=grape $ echo ${myArr[@]}


The index of the fourth element is discontinuous . We can get the subscripts of all assigned elements to view :
$ echo ${!myArr[@]}


Variables can also be used to index array elements :
$ declare -a myArr=(apple orange pear) $ i=2 $ echo ${myArr[$i]}


For indexed arrays , If you don't refer to array elements according to the rules, there may be some weird situations . such as , What happens when you index an element with a string instead of a non negative integer ?
$ declare -a myArr=(apple orange pear) $ echo ${myArr[hello]}


It's not always what you want . actually , Whatever string you write , Will be treated as 0 To deal with .

Add array element

A common array operation is to add new elements at the end of an array ( On preciseness , This should be a feature of the queue !):
$ declare -a myArr=(apple orange pear) $ myArr=("${myArr[@]}" grape) $ echo
${myArr[@]}


Note the output of the last line , The index of the added elements is continuous .

It is also a common operation to join two arrays into one :
$ declare -a myAbc=(a b c) $ myArr=("${myArr[@]}" "${myAbc[@]}") $ echo
${myArr[@]}


Extract subarray

Extract partial array elements :
$ declare -a myArr=(a b c d e f g) $ echo ${myArr[@]:2:3}


Length of array

stay Bash in , The syntax of referencing array length is somewhat strange :
$ declare -a myArr=(apple orange pear) $ echo ${#myArr[@]}


Array length refers to the number of elements assigned :
$ myArr[10]=grape $ echo ${#myArr[@]}


We can also get the length of an element :



ha-ha , It looks like a two-dimensional array !

Traversal array elements

adopt for Statements can traverse elements in an array like other programming languages :
$ declare -a myArr=(apple orange pear) $ myArr[10]=grape $ for element in "
${myArr[@]}"; do echo $element done


Delete array element
$ declare -a myArr=(apple orange pear) $ unset myArr[1] $ echo ${myArr[1]}


Index is 1 Element of is already empty , In fact, the length of the array has changed :
$ echo ${#myArr[@]}
Now the length of the array has become 2.

Here's a more complicated one :
$ declare -a myArr=(a b c d e f) $ myArr=("${myArr[@]:0:3}" "${myArr[@]:4}")
The above code removes the element d:



Empty array
$ declare -a myArr=(apple orange pear) $ unset myArr $ echo ${myArr[@]}


OK, There are no elements in the array !