Shell Arrays
Arrays can store multiple values. Bash Shell supports only one-dimensional arrays (does not support multi-dimensional arrays), and initialization does not require defining the array size (similar to PHP).
Similar to most programming languages, array elements are indexed starting from 0.
Shell arrays are represented using parentheses, with elements separated by "space" symbols, following this syntax:
array_name=(value1 value2 ... valuen)
Example
Create a simple array my_array:
#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org
my_array=(A B "C" D)
We can also define arrays using numerical indices:
array_name[0]=value0
array_name[1]=value1
array_name[2]=value2
Reading Array Elements
The general format for reading array element values is:
${array_name[index]}
The following example reads array elements by their numerical indices:
#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org
my_array=(A B "C" D)
echo "The first element is: ${my_array[0]}"
echo "The second element is: ${my_array[1]}"
echo "The third element is: ${my_array[2]}"
echo "The fourth element is: ${my_array[3]}"
Running the script will output:
$ chmod +x test.sh
$ ./test.sh
The first element is: A
The second element is: B
The third element is: C
The fourth element is: D
Associative Arrays
Bash supports associative arrays, which can use arbitrary strings or integers as indices to access array elements.
Associative arrays are declared using the declare
command, following this syntax:
declare -A array_name
The -A option is used to declare an associative array.
Keys in associative arrays are unique.
The following example creates an associative array site with different key-value pairs:
declare -A site=(["google"]="www.google.com" ["tutorialpro"]="www.tutorialpro.org" ["taobao"]="www.taobao.com")
We can also declare an associative array first and then set keys and values:
declare -A site
site["google"]="www.google.com"
site["tutorialpro"]="www.tutorialpro.org"
site["taobao"]="www.taobao.com"
Accessing associative array elements can be done using the specified key, in the following format:
array_name["index"]
The following example accesses elements of the associative array by their keys:
declare -A site
site["google"]="www.google.com"
site["tutorialpro"]="www.tutorialpro.org"
site["taobao"]="www.taobao.com"
echo ${site["tutorialpro"]}
Running the script will output:
www.tutorialpro.org
Getting All Elements in an Array
Using @
or *
can retrieve all elements in an array, for example:
#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org
my_array[0]=A
my_array[1]=B
my_array[2]=C
my_array[3]=D
echo "Array elements are: ${my_array[*]}"
echo "Array elements are: ${my_array[@]}"
Running the script will output:
$ chmod +x test.sh
$ ./test.sh
Array elements are: A B C D
Array elements are: A B C D
declare -A site
site["google"]="www.google.com"
site["tutorialpro"]="www.tutorialpro.org"
site["taobao"]="www.taobao.com"
echo "Array elements are: ${site[*]}"
echo "Array elements are: ${site[@]}"
Running the script will output:
$ chmod +x test.sh
$ ./test.sh
Array elements are: www.google.com www.tutorialpro.org www.taobao.com
Array elements are: www.google.com www.tutorialpro.org www.taobao.com
The elements of the array are: www.google.com www.tutorialpro.org www.taobao.com
Adding an exclamation mark !
before the array can retrieve all the keys of the array, for example:
Example
declare -A site
site["google"]="www.google.com"
site["tutorialpro"]="www.tutorialpro.org"
site["taobao"]="www.taobao.com"
echo "The keys of the array are: ${!site[*]}"
echo "The keys of the array are: ${!site[@]}"
Executing the script, the output is as follows:
The keys of the array are: google tutorialpro taobao
The keys of the array are: google tutorialpro taobao
Getting the Length of an Array
The method to get the length of an array is the same as getting the length of a string, for example:
Example
#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org
my_array[0]=A
my_array[1]=B
my_array[2]=C
my_array[3]=D
echo "The number of array elements is: ${#my_array[*]}"
echo "The number of array elements is: ${#my_array[@]}"
Executing the script, the output is as follows:
$ chmod +x test.sh
$ ./test.sh
The number of array elements is: 4
The number of array elements is: 4