In Bash, you can create arrays to store a collection of elements. Bash arrays are one-dimensional collections. In programming languages such as C and C++, arrays are collections of elements of the same data type. However, in Bash, depending on the use case, you can store integers and strings in the same array. In this tutorial, you’ll learn how to declare a Bash array and access its elements. You’ll then learn how to loop through the elements of an array and append elements to an array. Finally, you’ll learn about associative arrays in Bash. Let’s begin! ⚙ Interested in coding along? Here are the prerequisites:
If you are on a Mac or a Linux machine, you can open up your terminal and code along. If you are on a Windows machine, please install Git Bash or the Windows Subsystem for Linux (WSL2).
How to Declare a Bash Array
In this section, you’ll learn the syntax to declare a Bash array and code examples.
Syntax of Bash Array Declaration
You can declare an array in Bash using the following syntax: In the above syntax, arrayName denotes the name of the array that contains N elements elt1…eltN. The elements of an array are enclosed between the opening and closing parentheses (). Also, notice that the elements of the array are separated by a whitespace. As with any Bash variable, there should be no whitespace before or after the assignment operator =. 📑 In most other programming languages, you’ll separate the elements of an array or similar collections by commas. But in Bash, whitespace is the separator. In Bash, such arrays in which the elements are identified by their index is referred to as indexed array.
Declaring Indexed Arrays in Bash
Let’s create prices, an array of numbers. In Bash, you can use the echo command to print out the value of a variable. Using $variableName prints out the value of variableName. However, you can see that using the name of the array prints out only the first element. Bash arrays follow zero indexing. So the first element is at index 0, the second element is at index 1, and so on. You can also use negative indexing; the index of the last array element is -1. What happens if you try to access the element at a particular index and print it out? Let’s try printing out the element at index 1. Why is the output 24[1]? 🤔 This is because Bash substitutes $prices with 24, the first element in the array and [1] is printed out as such. To print out the element at a particular index, you can use the parameter expansion of the form ${prices[index]}. To print out all the elements in the array, you can specify @ instead of a specific index. Next, let’s create an array of strings. Running the following command creates an indexed array: The option -a creates an indexed array. You can now populate the array, one element at a time, by declaring the element at a specific index, as shown below: Now, to print out all the elements of the array, you can use ${fruits[@]}.
Access the Elements of a Bash Array
You can access the elements of the array in the following ways:
Loop through the array and access the element directly Loop through the set of indexes and access elements at a specific index
Loop Through the Array and Access Elements
If you’ve coded in Python, you’ll have used the for loop using the following syntax: Now let us write the Bash equivalent of the above for loop. We know that {prices[@]} expands to all the elements in the prices array. And ${prices[@]} gives us the values of all the elements. The Bash for loop is similar to Python’s for loop syntax, but the statements in the loop body should be enclosed within do and done, as shown: Because we use the echo command and print out the value of the variable price, we get the following output:
Access the Elements Using the Array Index
Another way to loop through arrays is using the index of the elements. This is similar to the for loop construct in Python is using the range() function: To get a list of indexes to loop through, you can place an exclamation point (!) before the array name in the parameter expansion. This will give you the list of all valid indexes for the array, as shown: The prices array contains 5 elements, so the index starts at 0 and goes up to 4. Next, we can loop through the list of indexes and access the element at each index. For index i, ${prices[i]} is the element at the index i. The above loop prints out all the elements in the array.
Append Elements to a Bash Array
In the prices array, we have five elements (at indexes 0,1,2,3 and 4). If you want to add an element to the end of the array at index 5, you can do so as follows: We see that 21 has been appended to the end of the prices array. However, it is more convenient to append to the end of the array—without remembering—the index of the last added element or the number of elements currently in the array. You can use arrayName+=(elements(s)) it to append one or more elements to an array, as shown: Now, if we print out the prices array, we see that 10 has been appended to the end of the array. Next, let’s learn how to declare associative arrays in Bash.
Associative Arrays in Bash
If you want to define a relationship in terms of key-value pairs, you can use an associative array. You can declare an associative array using the following general syntax. Notice that we use the -A option instead of -a. You can add elements to the associative array by specifying the key and the corresponding value. Here, we have added the names of the fruits as the keys and the numbers from the prices array as the values. So how do we access the elements in an associative array? Just the way you’d look up the value using the corresponding key in a Python dictionary, you can access the values in an associative array using the keys. We see that ${fruits_prices[@]} expands to the values and ${!fruits_prices[@]} expands to the keys. You can also loop through the associative array and access the keys, as shown: The following loop shows how you can access the values. Though it is recommended to use arrayName[key]=value, you can also declare it as a sequence like this: This way, Bash infers the first element as the first key, the second element as the first value, and so on.
Summing Up
I hope you now understand how to create and work with Bash arrays. Here’s a quick review of what you have learned.
You can declare an indexed array in Bash using the syntax arrayName=(elt1 elt2 elt3 … eltN) or run declare -a arrayName and add elements to the array. To access the elements, you can loop through using ${arrayName[@]}. Alternatively, you can get the list of all valid indexes using the parameter expansion ${!arrayName[@]}. Finally, you also learned how to declare an associative array to store key-value pairs in Bash.
Next, check out the tutorial for loops in Bash.