What Are Bash Dictionaries On Linux And How Are They Used?

Bash dictionaries provide you with 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 with a different name

The formal name for dictionaries is associative arrays. They are also called hash tables and hash maps. They are a data structure that works similarly to a regular array, but with one major difference.

An array is a collection of data values ​​that are stored in a data structure. To access any of the data values ​​known as array elements, you need to know their location in the array. The position of an array element in the array is called an index, so these types of arrays are called indexed arrays. They are the most common type of array.

They do have one drawback, however. How do you access an array element if you don’t know its position in the list? You have to iterate through all the elements in the array and test that the value at that point is what you are looking for.

Associative arrays solve this problem. They do not use whole numbers to uniquely identify array elements. They use unique words known as keywords. You can use its keyword to get the value for an array element regardless of where it is positioned within the array. For an indexed array, the integers that represent 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 getting its associated value mimics looking up a word in a dictionary and finding its meaning. For this reason, associative arrays are called dictionaries.

Bash 4.0 or higher

Associative arrays are supported in Bash Shell version 4.0 or higher. If you’re using an up-to-date Linux distribution you should be fine. To check your bash version use this command:

bash --version


The computer where this article was researched 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 declare bash command. the -A The (associative) option tells Bash that this is an associative array, not an indexed array.

declare -A acronyms

This creates an associative array called “Acronyms”.

In order to put some data in our array we need to provide keywords and values. We can do this with 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"

These commands define five array elements. Note that the values ​​are enclosed in quotation marks if the value contains spaces. Our keywords have been entered in alphabetical order but can be entered in any order. The keywords must be unique. If you try to create two entries with the same keyword, the second entered value will overwrite the first. You still only have one entry with that keyword associated with the second value added.

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

Use loops

Arrays are very useful for use in loops. Associative arrays are no exception. Loops are an efficient way to invoke a sequence of actions without repeating chunks of code. Before we look at loops, there is an efficient way to declare arrays.

We create the arrays with 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 name of the array is “Countries” and is linked to the list of values ​​by an equal sign. =. “The list of values ​​is enclosed in brackets”()“And each keyword is enclosed in brackets”[]“. Note that the values ​​are not separated by commas. If you have a value string that contains spaces, you need to enclose it in quotation marks.

To make an associative array return a keyword instead of the value, add an exclamation mark “!“In front of the array name. The at symbol “@”Can be used as a placeholder, ie all array elements.

this for loop lists all keywords:

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

Note that the keywords are not necessarily listed in the order they were created, but it doesn’t matter. Associative arrays do not rely on an ordered index.


We can also use the parameter extension to list all keywords. They are listed on one line, not one per line.

echo "${!countries[@]}"
echo "${!acronyms[@]}"

We can do ours for Loop to output 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 are in the arrays, we can use a hash “#” in front of the array name instead of an exclamation mark.

echo "${!countries[@]}"
echo "${!acronyms[@]}"

Check whether there is an array element

If you are looking for a keyword but there is no such array element, the return value is an empty string. Sometimes it is useful to have some other indicator of the presence or absence of an array element.

We can tell the presence of an array element with the “+_“Operator. Notice that this comes after the keyword, not before the array name, like the previous operators we saw.

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 have to define the size of your array when you declare it. You can always add new elements without reaching 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 a search 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 contains spaces, enclose 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 set a variable key to the string “EOF”. We use the variable when we add a new array element to the array. In fact, we are replacing the array element that we deleted earlier.

We get the new array element by calling it with the new keyword and also using the variable. If the keyword contains spaces, you must enclose the variable name in quotation marks.

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

Get creative

Our examples were collections of information where each array element is independent from all others, much like a dictionary. Each is a unique definition. But associative arrays can just as easily contain a variety of information all related to one thing, such as the specifications of various 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]}

Writing efficiently in a language means knowing the mechanisms and structures it offers and choosing the most appropriate for the problem being solved. Associative arrays provide an easy-to-use way for you to store data that you can search by name like a dictionary.

Related Posts