Shell Arrays

An array can store multiple values. Bash Shell only supports one-dimensional arrays (does not support multi-dimensional arrays). You do not need to define the array size during initialization (similar to PHP).

Similar to most programming languages, array element subscripts start from 0.

Shell arrays are represented by parentheses, and elements are separated by "space" symbols. The syntax format is as follows:

array_name=(value1 value2 ... valuen)

Example

Create a simple arraymy_array:

Example

#!/bin/bash
# author: Rookie Tutorial
# url:www.example.com

my_array=(A B "C" D)

We can also use numeric subscripts to define an array:

Example

array_name[0]=value0
array_name[1]=value1
array_name[2]=value2

Reading Arrays

The general format for reading array element values is:

${array_name[index]}

The following example reads array elements by numeric index:

Example

#!/bin/bash
# author: Rookie Tutorial
# url:www.example.com

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]}"

Execute the script, and the output result is as follows:

$ chmod +x test.sh 
$ ./test.sh
第一个元素为: A
第二个元素为: B
第三个元素为: C
第四个元素为: D

Associative Arrays

Bash supports associative arrays, where arbitrary strings or integers can be used as subscripts to access array elements.

Associative arrays usedeclarecommand to declare, the syntax format is as follows:

declare -A array_name

-AThe option is used to declare an associative array.

The keys of an associative array are unique.

In the following example, we create an associative arraysite, and create different key-value pairs:

Example

declare -A site=(["google"]="www.google.com" ["example"]="www.example.com" ["taobao"]="www.taobao.com")

We can also declare an associative array first, and then set the keys and values:

Example

declare -A site
site["google"]="www.google.com"
site["example"]="www.example.com"
site["taobao"]="www.taobao.com"

You can also assign values while defining:

To access associative array elements, you can use the specified key. The format is as follows:

array_name["index"]

In the following example, we access the elements of an associative array by key:

Example

declare -A site
site["google"]="www.google.com"
site["example"]="www.example.com"
site["taobao"]="www.taobao.com"

echo ${site["example"]}

Execute the script, and the output result is as follows:

www.example.com

Getting All Elements in an Array

Use@or*to get all elements in the array, for example:

Example

#!/bin/bash
# author: Rookie Tutorial
# url:www.example.com

my_array[0]=A
my_array[1]=B
my_array[2]=C
my_array[3]=D

echo "The elements of the array are:${my_array[*]}"
echo "The elements of the array are:${my_array[@]}"

Execute the script, and the output result is as follows:

$ chmod +x test.sh 
$ ./test.sh
数组的元素为: A B C D
数组的元素为: A B C D

Example

declare -A site
site["google"]="www.google.com"
site["example"]="www.example.com"
site["taobao"]="www.taobao.com"

echo "The elements of the array are:${site[*]}"
echo "The elements of the array are:${site[@]}"

Execute the script, and the output result is as follows:

$ chmod +x test.sh 
$ ./test.sh
数组的元素为: www.google.com www.example.com www.taobao.com
数组的元素为: www.google.com www.example.com www.taobao.com

Add an exclamation mark before the array!to get all keys of the array, for example:

Example

declare -A site
site["google"]="www.google.com"
site["example"]="www.example.com"
site["taobao"]="www.taobao.com"

echo "The keys of the array are:${!site[*]}"
echo "The keys of the array are:${!site[@]}"

Execute the script, and the output result is as follows:

数组的键为: google example taobao
数组的键为: google example taobao

Getting the Length of an Array

The method for getting the array length is the same as the method for getting the string length, for example:

Example

#!/bin/bash
# author: Rookie Tutorial
# url:www.example.com

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[@]}"

Execute the script, and the output result is as follows:

$ chmod +x test.sh 
$ ./test.sh
数组元素个数为: 4
数组元素个数为: 4
Other Extensions