>Arrays_
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: This will create an array, called
Thus we can access the string
If we want to specify a particular index for our values when creating the array we can do it like this: As you might have guessed, this will create an array with
If our indices are all sequential and we just want to change the starting index, we can use 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",
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: 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 to add an element to the array (or modify it if it exists already) and to remove one.
The
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: is equivalent to
Further reading: the bash reference manual
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: This will create an array, called
names
, which holds 4 values.
The values in the array can be accessed using the syntax
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: 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 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: 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 to add an element to the array (or modify it if it exists already) and 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: is equivalent to
"Mario" "Luigi"
, while
counts the number of elements in the array, in this case 3.Further reading: the bash reference manual