--- layout: page category-page: scripts category-title: Scripting tags: array variable many index selector author: Marco Tereh title: Arrays previous-page: pages/scripts/parameter-expansion.html next-page: while-loop.html --- There is a special kind of variable, called an array.
While variables hold a single value, arrays hold many. They could be called a list of variables. The simplest way to create an array is using this format: {% highlight bash %} names=("Anna" "Bob" "Clarice" "Dee") {% endhighlight %} This will create an array, called names, which holds 4 values. The values in the array can be accessed using the syntax {% highlight bash %} ${names[SELECTOR]} {% endhighlight %} where SELECTOR is the selector associated to the desired element. There are two kinds of arrays, depending on what kind of selector is used.
names is an indexed array, meaning the selector is a number. Since we didn't specify any when we defined it, names' selectors start at 0 and count up from there.
Thus we can access the string "Anna" using names[0]. In the same way we use names[1] to access "Bob" and so on.
If we want to specify a particular index for our values when creating the array we can do it like this: {% highlight bash %} names=([1]="Anna" [2]="Bob" [5]="Ernie" [12]="Luigi") {% endhighlight %} As you might have guessed, this will create an array with "Anna" at index 1, "Bob" at index 2, "Ernie" at index 3 and so on.
If our indices are all sequential and we just want to change the starting index, we can use {% highlight bash %} names=([12]="Luigi" "Mario" "Nate") {% endhighlight %} Which will create an array with "Luigi" at index 12, "Mario" at index 13 and "Nate" at index 14.
An indexed array's selector can also be negative, which will start counting from the end, so ${names[-1]} means "Nate", ${names[-3] is "Luigi" and so on.
The other kind of array is an associative array. In an associative array, the values are not mapped to a number but to some other string.
Here's an example of an associative array: {% highlight bash %} colors=([white]="#FFFFFF" [black]="#000000" [red]="#FF0000" [yellow]="#00FFFF") {% endhighlight %} In this case it is not possible to omit the selector as there is no logical word that "follows" white. To access its members, we use the same syntax but write a string instead of a number as the selector.
Arrays, both kinds, can also be modified after creation. We can use {% highlight bash %} colors[blue]="#0000FF" {% endhighlight %} to add an element to the array (or modify it if it exists already) and {% highlight bash %} unset colors[black] {% endhighlight %} to remove one.

The @ and * selectors are special - they cannot be written to but reading from them will return all the values in the array, separated by spaces. Similarly, ${!colors[@]} is a list of all the selectors (or indices) that were assigned a value.

Parameter Expansion works on indexed arrays as well, much like it works on strings, but it manipulates the members of the array instead of characters. For example: {% highlight bash %} ${names[@]: 13} {% endhighlight %} is equivalent to "Mario" "Luigi", while {% highlight bash %} ${#names[@]} {% endhighlight %} counts the number of elements in the array, in this case 3.

Further reading: the bash reference manual