Previous | Table of Contents | Next |
Although this works fine for small numbers of items, the array notation is much more efficient for large numbers of items. If you have to write a script using only sh, you can use this method for simulating arrays.
In the previous example, you set the array indices in sequence, but this is not necessary. For example, if you issue the command
$ FRUIT[10]=plum
the value of the item at index 10 in the array FRUIT is set to plum. One thing to note here is that the shell does not create a bunch of blank array items to fill the space between index 2 and index 10. Instead, it keeps track of only those array indices that contain values.
Caution:
In ksh, numerical indices for arrays must be between 0 and 1,023. In bash there is no such requirement.In addition, both ksh and bash support only integer array indices. This means that you cannot use floating point or decimal numbers such as 10.3.
If an array variable with the same name as a scalar variable is defined, the value of the scalar variable becomes the value of the element of the array at index 0. For example, if the following commands are executed
$ FRUIT=apple $ FRUIT[1]=peach
the element FRUIT has the value apple. At this point any accesses to the scalar variable FRUIT are treated like an access to the array item FRUIT[0].
The second form of array initialization is used to set multiple elements at once. In ksh, this is done as follows:
set A name value1 value2 ... valuen
In bash, the multiple elements are set as follows:
name=(value1 ... valuen)
Here, name is the name of the array and the values, 1 to n, are the values of the items to be set. When setting multiple elements at once, both ksh and bash use consecutive array indices beginning at 0. For example the ksh command
$ set A band derri terry mike gene
or the bash command
$ band=(derri terry mike gene)
is equivalent to the following commands:
$ band[0]=derri $ band[1]=terry $ band[2]=mike $ band[3]=gene
Tip:
When setting multiple array elements in bash, you can place an array index before the value:myarray=([0]=derri [3]=gene [2]=mike [1]=terry)The array indices dont have to be in order, as shown previously, and the indices dont have to be integers.
This feature is not present in ksh.
After you have set any array variable, you access it as follows:
${name[index]}
Here name is the name of the array, and index is the index that interests us. For example, if the array FRUIT was initialized as given previously, the command
$ echo ${FRUIT[2]}
produces the following output:
orange
You can access all the items in an array in one of the following ways:
${name[*]} ${name[@]}
Here name is the name of the array you are interested in. If the FRUIT array is initialized as given previously, the command
$ echo ${FRUIT[*]}
produces the following output:
apple banana orange
If any of the array items hold values with spaces, this form of array access will not work and will need to use the second form. The second form quotes all the array entries so that embedded spaces are preserved.
For example, define the following array item:
FRUIT[3]="passion fruit"
Assuming that FRUIT is defined as given previously, accessing the entire array using the following command
$ echo ${FRUIT[*]}
results in five items, not four:
apple banana orange passion fruit
Commands accessing FRUIT using this form of array access get five values, with passion and fruit treated as separate items.
To get only four items, you have to use the following form:
$ echo ${FRUIT[@]}
The output from this command looks similar to the previous commands:
apple banana orange passion fruit
but the commands see only four items because the shell quotes the last item as passion fruit.
The shell provides a way to mark variables as read-only by using the readonly command. After a variable is marked read-only, its value cannot be changed.
Consider the following commands:
$ FRUIT=kiwi $ readonly FRUIT $ echo $FRUIT kiwi $ FRUIT=cantaloupe
The last command results in an error message:
/bin/sh: FRUIT: This variable is read only.
As you can see, the echo command can read the value of the variable FRUIT, but the shell did not enable us to overwrite the value stored in the variable FRUIT.
This feature is often used in scripts to make sure that critical variables are not overwritten accidentally.
In ksh and bash, the readonly command can be used to mark array and scalar variables as read-only.
Unsetting a variable tells the shell to remove the variable from the list of variables that it tracks. This is like asking the shell to forget a piece of information because it is no longer required.
Both scalar and array variables are unset using the unset command:
unset name
Here name is the name of the variable to unset. For example,
unset FRUIT
unsets the variable FRUIT.
You cannot use the unset command to unset variables that are marked readonly.
Previous | Table of Contents | Next |