Though Bash is sometimes thought of as just a scripting language, it is technically a programming language, with many of the bells and whistles of a more full-blown programming language to boot. One of Bash’s gifts to a would-be programmer is the Bash array. While Bash arrays aren’t necessarily as robust as other languages they can be incredibly useful.
The Basics
Let’s get started with a few examples.
Creating a Simple Array
my_array=(apple orange banana)
echo ${my_array[0]}
echo ${my_array[1]}
echo ${my_array[2]}
Line 1: Creates an array called my_array and assigns it the values: apple, orange, and banana.
By default, bash uses any spaces, tabs, or newlines1A new line in bash is signfied by “\n”. between the values as a “delimiter” to decide where a value begins and ends.2Just for your general awareness, by modifying the internal field seperator or IFS we can tell bash to use delimeters other than a spaces, tabs, or newlines. For instance, you may want to use a comma as your delimiter. Or you may want to modify the delimiter to ensure that newlines are not used as delimiters. Because this is an introductory post on Bash arrays and in most cases the default IFS configuration is completely suitable, I won’t be covering IFS modification here.
Lines 3-5: Outputs the contents of the the associated element to the screen. Each element is denoted by a number. In this case they are 0, 1, and 2.
When ran the script above produces the following output:
apple
orange
banana
Bash, like many other languages, uses zero indexed arrays. Basically, this means that the first element in the array is assigned an index of 0. The second element is assigned to the index 1 and so forth for all the elements in the array. As a visual aid, bash array indexes are arranged like this:
A Quick Word on Bash Syntax
When I first began writing code in Bash, something that threw me off was the syntax between using a variable in a standard way and using a variable that is an array. For example:
my_array=(horse dog banana)
echo $my_array
horse
In the above situation, it might be expected that echoing out the variable array with “echo $array” would result in the full array to be output to the screen. But the actual syntax we need to print the full array is as follows:
my_array=(horse dog banana)
echo ${my_array[@]}
horse dog banana
I hope to explain this more fully in a later post, but for now it’s enough to just remember that using Bash arrays and braces go hand in hand.
For Loops and Arrays
One of the most common ways to leverage an array is by using a for loop. This is often useful for larger arrays or arrays where the length is not necessarily known. For example, if the data we are using to populate the array is from a large file or a file that could change. Let’s look at a very basic example of a for loop using an array:
my_array=(horse dog banana)
for item in "${my_array[@]}";
do
echo "$item"
done
Line 1: Creates an array called my_array and assigns it the values: horse, dog, and banana.
Line 3: Creates the for loop’s control variable3For a basic definition of control variables, see here. “elem” and tells the system to use the items in “my_array” within the loop.
Line 4: This is a mandatory statement that signifies the beginning of the loop. The “do” keyword could also be placed after the semicolon on line 3, for example: for item in "${my_array[@]}"; do
Line 5: In this case, this line simply prints each element in the array to the screen. However, this is an important line because it is where any logic we want to add to the loop would begin. For example, I may want to add an if statement that causes the loop to output “$item” or set another variable only if “$item” equals banana.
Line 6: This is another mandatory statement that signifies the end of the loop. If we wanted to, with shorter loops like this one we could write the entire loop on one line like so: for item in "${my_array[@]}"; do echo "$item"; done
This is mostly just a stylistic decision, but sometimes it can be useful as a space saving technique within a script. Just remember to place semicolons after each command.
Final Tips and Tricks
Here are some general tips and tricks that are useful when working with Bash arrays. This is by no means an exhaustive list. I haven’t covered associative arrays in this post, which can come in handy sometimes as well. But I hope that what I have covered here will at least get you started with Bash arrays.
# Get the number of items in the array (or length)
${#array[@]}
# Append a value to the array (becomes last element)
array+=(value)
# Create an array using the output of the command "ls".
# This can be used with any command where the output makes sense.
array=( $(ls) )