A Rose by Any Other Name
An array is a collection of data values held in one data structure. To access any of the data values, which are known as array elements, you need to know their position in the array. An array element’s position in the array is known as its index, so these types of arrays are known as indexed arrays. They’re the most commonly used type of arrays.
However, they do have a drawback. How do you access an array element if you don’t know its position within the list? You need to run through all of the elements in the array, testing to see whether the value at that location is the one that you’re looking for.
Associative arrays overcome that issue. They don’t use integers to uniquely identify array elements. They use unique words known as keywords. You can retrieve the value for an array element by using its keyword, regardless of where it’s positioned within the array. With an indexed array, the integer numbers representing the positions within the array are in ascending order. The keywords in an associative array can be in any order.
You can look up a value in an associative array by searching with its keyword. Looking up a word and retrieving its associated value mimics looking up a word in a dictionary and finding its meaning. That’s why associative arrays are known as dictionaries.
Bash 4.0 or Higher
Associative arrays are supported in the Bash shell version 4.0 or higher. If you’re using a current Linux distribution, you should be fine. To check your Bash version, use this command:
The machine used to research this article has Bash 5.1.4 installed, so we’re good to go.
Basic Principles
To create an associative array on the terminal command line or in a script, we use the Bash declare command. The -A (associative) option tells Bash that this will be an associative array and not an indexed array.
This creates an associative array called “acronyms.”
To put some data into our array, we need to provide keywords and values. We can do this using this format:
Let’s add some array elements:
Those commands define five array elements. Note that the values are wrapped in quotation marks if there are spaces in the value. Our keywords were entered in alphabetical order, but they can be entered in any order you like. The keywords must be unique. If you try to create two entries with the same keyword, the second value that you enter will overwrite the first. You’ll still only have one entry with that keyword, and it will be associated with the second value that you added.
To retrieve values from the array, we use commands in this format:
We can use echo to send the output to the terminal window:
Using Loops
Arrays lend themselves to being used in loops very well. Associative arrays are no exception. Loops provide efficient ways to invoke a sequence of actions without repetitive sections of code. Before we look at loops, there’s an efficient way to declare arrays.
We create the arrays using the declare command (the same -A option as before), but we provide the keywords and values as a list on the command line.
The array name is “countries,” and it’s connected to the value list by an equals sign ” =.” The value list is wrapped in parentheses “()” and each keyword is wrapped in brackets “[]“. Note that there are no commas separating the values. If you have a value string that contains spaces, you’ll need to wrap it in quotation marks.
To make an associative array return a keyword instead of the value, add an exclamation point “!” in front of the array name. The at symbol “@” can be used as a wildcard, meaning all array elements.
This for loop will list all of the keywords:
Note that the keywords aren’t necessarily listed in the order that they were created, but that doesn’t matter. Associative arrays don’t rely on an ordered index.
We can also use parameter expansion to list all of the keywords. They’ll be listed on one line, not one per line.
We can augment our for loop to print the keywords and values at the same time.
If we want to know how many elements there are in the arrays, we can use a hash “#” in front of the array name instead of an exclamation point.
Checking That an Array Element Exists
If you search for a keyword but there is no such array element, the return value will be an empty string. Sometimes it’s useful to have a different indicator for the presence or absence of an array element.
We can check for the presence of an array element using the “+_” operator. Note that this comes after the keyword, not in front of the array name like the previous operators we’ve seen.
The array element with the keyword “EOF” is found in the array, but the array element with the keyword “FTP” is not.
Adding Array Elements
Adding new elements to an associative array is easy. Unlike some programming languages, you don’t need to define the size of your array when you declare it. You can keep adding new elements without hitting a predefined upper limit.
To add a new element to the array, we use the “+=” operator.
The number of elements in the array is now six, and searching for the new keyword finds the array element and returns its value.
Removing Array Elements and Arrays
The unset command is used to remove array elements. If the keyword has spaces in it, wrap it in quotation marks.
To remove the entire array, use unset with the name of the array.
Using Variables with Arrays
Using variables with associative arrays is straightforward. In this example, we’ll set a variable key to the string “EOF.” We’ll use the variable when we add a new array element to the array. Actually, we’re replacing the array element that we deleted earlier.
We’ll retrieve the new array element by calling it with the new keyword and also, by using the variable. If the keyword contains spaces, you’ll need to wrap the variable name in quotation marks.
Get Creative
Our examples have been collections of information where each array element is independent of all of the others, much like a dictionary. Each one is a unique definition. But associative arrays can just as easily hold a variety of pieces of information all related to one thing, such as the specifications of different pieces of computer hardware:
Writing efficiently in a language means knowing the mechanisms and structures that it offers, and selecting the most appropriate one for the problem that you’re trying to solve. Associative arrays give you an easy-to-use way of storing data that you can search by name, just like a dictionary.