A stylized laptop showing a terminal with lines of text.
fatmawati achmad zaenuri/Shutterstock.com

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

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

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

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:

acronyms[BGP]="Border Gateway Protocol"
acronyms[CIDR]="Classless Inter-Domain Routing"
acronyms[DHCP]="Dynamic Host Configuration Protocol"
acronyms[EOF]="End of Frame"

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:

echo ${acronyms[ACK]}
echo ${acronyms[DHCP]}

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.

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

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[@]}"

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

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[@]}"

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

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]}

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

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

unset countries

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.

acronyms[$key]="End of Frame"
echo ${acronyms[EOF]}
echo ${acronyms[$key]}

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"
echo ${specification[CPU]}

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.

Profile Photo for Dave McKay Dave McKay
Dave McKay first used computers when punched paper tape was in vogue, and he has been programming ever since. After over 30 years in the IT industry, he is now a full-time technology journalist. During his career, he has worked as a freelance programmer, manager of an international software development team, an IT services project manager, and, most recently, as a Data Protection Officer. His writing has been published by  howtogeek.com, cloudsavvyit.com, itenterpriser.com, and opensource.com. Dave is a Linux evangelist and open source advocate.
Read Full Bio »