
Welcome to the fourth part of the Bash Bonanza series!
In the previous entry, we discussed how to use functions in Bash, and finished off with a spooky warning about arrays, and how they will not work with the techniques discussed so far.
Today we will explore that further.
Array Syntax
Bash has two types of arrays - indexed arrays (standard array) and key-value associative arrays (hash).
Array declaration
Note that there are other ways to create indexed arrays, but you must use the declare syntax to create associative arrays. To avoid confusion, let's use this syntax for both.
Note that declare will automatically make the variable local if defined in a function.
Array initialization
This is done with parentheses for both types of arrays. For indexed arrays, keys are surrounded with square brackets to seperate them from from the value.
For both types of arrays, referencing an element is done with the square brackets syntax.
Array element access and assignment
These are both done with the square brackets as follows.
Special indices
You can also use '@' and '*' inside square brackets. These will output the whole array. The difference is that '*' will output the array as a single string, while '@' will output each individual word separately.
Accessing keys
This can be done using the above special indices with the addition of an exclamation mark as follows.
Passing arrays to functions
This, as hinted before, is unfortunately more tricky than it should be.
The difficulty occurs because unlike in many other languages, referring the array variable will return the first element of the array instead of the whole array.
As a result, you cannot simply refer to the variable and pass it around functions. Instead, you will have to pass around the array name.
For this to work correctly, we will have to use the concept of an indirect reference in bash - basically fetching the value of a value. This is done by preceding the variable name with an exclamation mark (not to be confused with the exclamation mark to show indices - that is, in fact, an exception!).
Using this, we can do the following - it even works with multiple arrays. Shout out to Ken Bertelson for this trick!
Final thoughts
If you have been writing bash, you may have seen $@ and $* used in order to get all the arguments passed to the bash script. These, in fact, behave like arrays, which you are now well equipped to deal with! The * and @ stand for the same thing as they do for arrays.
Happy bashing!
