Shell Arrays
An array can store multiple values. Bash Shell only supports one-dimensional arrays (not multidimensional arrays), and the array size need not be defined at initialization (similar to PHP).
Like most programming languages, array elements are indexed starting from 0.
Shell arrays are represented with parentheses, and elements are separated by "space" characters. The syntax is as follows:
array_name=(value1 value2 ... valuen)
Example
Create a simple array my_array:
Example
#!/bin/bash
author:Runoob Tutorial
url:www.runoob.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 an array element's value is:
${array_name[index]}
The following example reads array elements by numeric index:
Example
#!/bin/bash
author:Runoob Tutorial
url:www.runoob.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]}"
Run the script, and the output is as follows:
$ chmod +x test.sh
$ ./test.sh
第一个元素为: A
第二个元素为: B
第三个元素为: C
第四个元素为: D
Associative arrays
Bash supports associative arrays, which allow you to use arbitrary strings or integers as subscripts to access array elements.
Associative arrays use the declare command to declare; the syntax is as follows:
declare -A array_name
-A The option is used to declare an associative array.
The keys of an associative array are unique.
In the following example we create an associative array site, and create different key-value pairs:
Example
declare
-A
site
=
(
[
"google"
]
=
"www.google.com"
[
"runoob"
]
=
"www.runoob.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
[
"runoob"
]
=
"www.runoob.com"
site
[
"taobao"
]
=
"www.taobao.com"
You can also assign values at the same time as the definition:
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 associative array elements by key:
Example
declare
-A
site
site
[
"google"
]
=
"www.google.com"
site
[
"runoob"
]
=
"www.runoob.com"
site
[
"taobao"
]
=
"www.taobao.com"
echo
${site["runoob"]}
Run the script, and the output is as follows:
www.runoob.com
Getting all elements in an array
Use @ or * to get all elements in an array, for example:
Example
#!/bin/bash
author:Runoob Tutorial
url:www.runoob.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[@]}"
Run the script, and the output 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
[
"runoob"
]
=
"www.runoob.com"
site
[
"taobao"
]
=
"www.taobao.com"
echo
"The elements of the array are: ${site[*]}"
echo
"The elements of the array are: ${site[@]}"
Run the script, and the output is as follows:
$ chmod +x test.sh
$ ./test.sh
数组的元素为: www.google.com www.runoob.com www.taobao.com
数组的元素为: www.google.com www.runoob.com www.taobao.com
Adding an exclamation mark ! before the array gets all the keys of the array, for example:
Example
declare
-A
site
site
[
"google"
]
=
"www.google.com"
site
[
"runoob"
]
=
"www.runoob.com"
site
[
"taobao"
]
=
"www.taobao.com"
echo
"The keys of the array are: ${!site[*]}"
echo
"The keys of the array are: ${!site[@]}"
Run the script, and the output is as follows:
数组的键为: google runoob taobao
数组的键为: google runoob taobao
Getting the length of an array
The method for getting an array's length is the same as for getting a string's length, for example:
Example
#!/bin/bash
author:Runoob Tutorial
url:www.runoob.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[@]}"
Run the script, and the output is as follows:
$ chmod +x test.sh
$ ./test.sh
数组元素个数为: 4
数组元素个数为: 4