Quick Links

Bash dictionaries give you hash maps and associative arrays in Linux shell scripts. We'll show you how to use these powerful and useful data structures in your own Linux shell scripts.

A Rose by Any Other Name

Related: What Is the Bash Shell, and Why Is It So Important to Linux? The formal name for dictionaries is associative arrays. They're also called hash tables and hash maps. They're a data structure that functions similarly to a regular array, but with a significant difference.

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:

bash --version

bash --version in a terminal window

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.

declare -A acronyms

declare -A acronyms in a terminal window

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:

array-name[key]=Value

Let's add some array elements:

acronyms[ACK]=Acknowledgement

acronyms[BGP]="Border Gateway Protocol"

acronyms[CIDR]="Classless Inter-Domain Routing"

acronyms[DHCP]="Dynamic Host Configuration Protocol"

acronyms[EOF]="End of Frame"

acronyms[ACK]=Acknowledgement in a terminal window

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:

${array-name[key]}

We can use echo to send the output to the terminal window:

echo ${acronyms[ACK]}

echo ${acronyms[DHCP]}

echo ${acronyms[ACK]} in a 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.

declare -A countries=( [ALB]=Albania [BHR]=Bahrain [CMR]=Cameroon [DNK]=Denmark [EGY]=Egypt )

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.

declare -A countries=( [ALB]=Albania [BHR]=Bahrain [CMR]=Cameroon [DNK]=Denmark [EGY]=Egypt ) in a terminal window

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:

for key in "${!countries[@]}"; do echo $key; done

for key in "${!countries[@]}"; do echo $key; done in a terminal window

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.

echo "${!countries[@]}"

echo "${!acronyms[@]}"

echo "${!countries[@]}" in a terminal window

We can augment our for loop to print the keywords and values at the same time.

for key in "${!acronyms[@]}"; do echo "$key - ${acronyms[$key]}"; done

for key in "${!acronyms[@]}"; do echo "$key - ${acronyms[$key]}"; done in a terminal window

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.

echo "${!countries[@]}"

echo "${!acronyms[@]}"

echo "${!countries[@]}" in a terminal window

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.

if [ ${acronyms[EOF]+_} ]; then echo "Found"; else echo "Not found"; fi

if [ ${acronyms[FTP]+_} ]; then echo "Found"; else echo "Not found"; fi

if [ ${acronyms[EOF]+_} ]; then echo "Found"; else echo "Not found"; fi in a terminal window

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.

countries+=( [FJI]=Fiji )

echo "$(#countries[@]}"

echo ${countries[FJI]}

countries+=( [FJI]=Fiji ) in a terminal window

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.

unset acronyms[EOF]

if [ ${acronyms[EOF]+_} ]; then echo "Found"; else echo "Not found"; fi

unset acronyms[EOF] in a terminal window

To remove the entire array, use unset with the name of the array.

unset countries

unset countries in a terminal window

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.

key=EOF

acronyms[$key]="End of Frame"

echo ${acronyms[EOF]}

echo ${acronyms[$key]}

key=EOF in a terminal window

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:

declare -A specification

specification[CPU]="Dual Core AMD Ryzen 5 3600"

specification[Speed]="3600 MHz"

specification[Kernel]="5.11.0-17-generic x86_64"

specification[Mem]="1978.5 MiB"

specification[Storage]="32 GiB"

specification[Shell]="Bash"

echo ${specification[CPU]}

declare -A specification in a terminal window

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.