Array
A simple definition for an array is that it is a single variable (varname) that can store one or more values. The Korn shell has two types of arrays: indexed and associative.
In an indexed array, each element of the array is indexed with an arithmetic expression. The index is valid as long as the value of the arithmetic expression is at least zero. The upper value of the arithmetic expression is dependent on the implementation, but in all versions of Linux and UNIX, it is at least 4,095.
Note - On a sample Red Hat Linux 6.1 system, a simple program to increment an indexed array had a memory fault after 13088. This means that 13,088 values were stored in one array before a memory error caused the system to crash the program.
In an associative array, the subscript is an arbitrary string, such as "Yankees," "Red Sox," or "Tigers."
Declaring
Associative arrays must be declared with typeset -A:
$ typeset Ai Alteams # values will be integers $ Alteams["Yankees"]=92 $ Alteams["Red Sox"]=88
An indexed array does not have to be previously declared. If the programmer knows or wants to specify the maximum size of an indexed array, he can use the typeset -uvariable[n] command, where n is the upper bound of the array and variable is the name of the array, as seen in the following example:
typeset -u children[2]
There are two ways of assigning values to array elements, and there are two ways of accessing arrays. These are explained next.
AssigningTwo Ways
The first way of assigning a value to an array is similar to what would be done with scalars. Just as a scalar is a variable name with a single value associated with it, an individual array element is treated as a scalar. Using an array name without an element number in square brackets is the equivalent of referencing the zero element of the array. The syntax is varname=value, as shown in the following example:
$ Children="Meshia"
This example looks identical to a scalar. It could also have been written like this:
$ Children[0]="Meshia"
The arithmetic expression inside the square brackets indicates which array element is being referenced. In most computer languages, including shell programming, indexes begin with the zero value. Therefore, Children[2] actually is a reference to the third indexed value of Children.
The second way of assigning a value to an array is to assign multiple elements at the same time. The syntax is set -A varname value1 value2 ... valuen, as shown in the following example:
$ set -A Children Meshia Andy Ashley Tommy
Note - In other shells, such as bash, it is written slightly differently and looks like this:
Children=(Meshia Andy Ashley Tommy)
The following are a couple of items to note:
-
The arithmetic expression used for the index must be a positive integer. This means that it cannot be negative, nor can it have a decimal point.
-
The term arithmetic expression means that the value does not have to be a number, but can be a variable or even an equation, as long as the result of the expression is a positive integer.
The following two examples show using a variable for the indexvalue when assigning a value to an array:
$ Cmd[x]="hostname"$ Cmd[x]="uname -a"
These two examples are part of the CHECKIT program used in Chapter 13, "Pulling It All Together."
AccessingTwo Ways
Now that some values are assigned to the arrays, the next question becomes how to access the values. Both indexed and associative arrays are accessed the same way. Just as there are two ways of assigning values to arrays, there are also two ways of accessing those values.
The first way of accessing the value of an array is with the following syntax:
${varname[index or string]}
The following example uses the Children array assigned earlier and returns one of its values:
$ echo ${Children[2]} Ashley
The second way of accessing the value of an array is to access more than one value at a time. Two characters can be placed in the index portion of the command that allows this. These are shown here:
$ echo ${Children[*]} Meshia Andy Ashley Tommy $ echo ${Children[@]} Meshia Andy Ashley Tommy
You are thinking, and rightfully so, that these two commands produce the same output. The difference would have existed if one or more of the values had contained spaces. To further explain this, we will set up a new array for Children:
$ Children="Meshia Davidson" $ Children[1]="Ashley Davidson"
Now, if the array is accessed, a slightly different output is produced:
$ echo ${Children[*]} Meshia Davidson Ashley Davidson $ echo ${Children[@]} Meshia Davidson Ashley Davidson
In most versions of UNIX, the echoing of the children with the * produces four items. In contrast, in most versions of UNIX, the echoing of the children with the @ produces two items, as shown in the previous example. Linux's version of the Korn shell, however, produces four items either way.