13.15. Arrays
Versions of bash 2.x provide for creation of one-dimensional arrays. Arrays allow you to collect a list of words into one variable name, such as a list of numbers, a list of names, or a list of files. Arrays are created with the built-in function declare –a, or can be created on the fly by giving a subscript to a variable name, such as x[0]=5. The index value is an integer starting at 0. There is no maximum size limit on the array, and indices do not have to be ordered numbers, such as, x[0], x[1], x[2], and so on. To extract an element of an array, the syntax is ${arrayname[index]}. If declare is given the –a and –r options, a read-only array is created.
FORMAT
declare -a variable_name
variable = ( item1 item2 item3 ... )
Example 13.78.
declare -a nums=(45 33 100 65)
declare -ar names (array is readonly)
names=( Tom Dick Harry)
states=( ME [3]=CA CT )
x[0]=55
n[4]=100
When assigning values to an array, they are automatically started at index 0 and incremented by 1 for each additional element added. You do not have to provide indices in an assignment, and if you do, they do not have to be in order. To unset an array, use the unset command followed by the array name, and to unset one element of the array, use unset and the arrayname[subscript] syntax.
The declare, local, and read-only built-ins also can take the –a option to declare an array. The read command with the –a option is used to read in a list of words from standard input into array elements.
Example 13.79.
1 $ declare -a friends
2 $ friends=(Sheryl Peter Louise)
3 $ echo ${friends[0]}
Sheryl
4 $ echo ${friends[1]}
Peter
5 $ echo ${friends[2]}
Louise
6 $ echo "All the friends are ${friends[*]}"
All the friends are Sheryl Peter Louise
7 $ echo "The number of elements in the array is ${#friends[*]}"
The number of elements in the array is 3
8 $ unset friends or unset ${friends[*]}
EXPLANATION
The declare built-in command is used to explicitly declare an array, but it is not necessary. Any variable that uses a subscript, such as variable[0], when being assigned a value, will automatically be treated as an array. The array friends is assigned a list of values: Sheryl, Peter, and Louise. The first element of the friends array is accessed by enclosing the array name and its subscript in curly braces, with an index of 0 used as the value of the subscript. Sheryl is printed. The second element of the friends array is accessed by using the index value of 1. The third element of the friends array is accessed by using the index value of 2. When you place the asterisk within the subscript, all of the elements of the array can be accessed. This line displays all the elements in the friends array. The syntax ${#friends[*]} produces the size of the array (i.e., the number of elements in the array). On the other hand, ${#friends[0]} produces the number of characters in the value of the first element of the array. There are six characters in Sheryl. The unset built-in command deletes the whole array. Just one element of the array can be removed by typing unset friends[1]; this would remove Sheryl.
Example 13.80.
1 $ x[3]=100
$ echo ${x[*]}
100
2 $ echo ${x[0]}
3 $ echo ${x[3]}
100
4 $ states=(ME [3]=CA [2]=CT)
$ echo ${states[*]}
ME CA CT
5 $ echo ${states[0]}
ME
6 $ echo ${states[1]}
7 $ echo ${states[2]}
CT
8 $ echo ${states[3]}
CA
EXPLANATION
The third element of the array, x, is being assigned 100. It doesn't matter if the index number is 3, but because the first two elements do not exist yet, the size of the array is only 1. ${x[*]} displays the one element of the array, x. x[0] has no value, and neither do x[1] and x[2]. The states array is being assigned ME at index 0, CA at index 3, and CT at index 2. In this example, you can see that bash doesn't care at what index you store values, and that the index numbers do not have to be contiguous. The first element of the states array is printed. There is nothing stored in states[1]. The third element of the states array, states[2], was assigned CT. The fourth element of the states array, states[3], was assigned CA.
|