Question

Associative Arrays in Bash - What is an Associative Array and to Declare it

A little background:

So recently I was working a project where I had to implement some new features. The project was primarily written in BASH. While I love BASH it’s for simpler tasks, or at least this is what I thought.

What is an Associative array:

In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection.

I’ve used associative arrays in different projects when I had to work with PHP, Python, and other languages but never with BASH, I didn’t even know it was possible.

In this mini-tutorial, I’ll go over exactly that, associative arrays in BASH, how to control, populate it, how to define and use them.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
May 4, 2021
Accepted Answer

Declaring associative arrays

An associative array in bash is declared by using the declare -A command (How surprising, I know :D).

declare -A NameOfArray

For this example, we’ll use DOAssosArray

declare -A DOAssosArray

Initizialing/populating an associative array

Elements in the array can be initialized at the time of array declaration or after declaring the array variable

  • Populating array variables AFTER it’s declaration

In the previous paragraph, we declared the array DOAssosArray. In order to populate it, we can execute the following commands:

DOAssosArray[KEY]=VALUE

The variable inside the brackets - [] will be the KEY and the value after the equal sign will be the VALUE.

Let’s make a more real-life example. We’ll use Countries as KEYS and Capitals as VALUES.

DOAssosArray[Germany]=Berlin
DOAssosArray[France]=Paris
DOAssosArray[Spain]=Madrid

  • Populate the array on declaration

Let’s say we wanted to populate the associative array while we were creating it. This can be achieved by:

declare -A DOAssosArray ( [KEY]=VALUE [KEY]=VALUE [KEY]=VALUE )

or following our example like this:

declare -A DOAssosArray ( [Germany]=Berlin [France]=Paris [Spain]=Madrid )

Accessing elements in the Associative array

Elements in the associative array can be accessed individually or by using for and while loops.

  • Accessing elements individually

You can access values in an associative array by calling its KEY:

echo ${DOAssosArray[KEY]}

in our case

echo ${DOAssosArray[Germany]}

The output will be Berlin.

root@DevServer:~$ echo ${DOAssosArray[Germany]}
Berlin                

Since this mini-tutorial is becoming far too long, we’ll review only accessing associative array elements individually and in the next part work through the other possibilities and even try to explore more of how associative array in BASH work and how we can manage them.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel