Question

Associative Arrays in Bash - Accessing Elements in an Associative array

Hi all,

This is the second mini tutorial about Associative Arrays in Bash. You can check the previous one here:

https://www.digitalocean.com/community/questions/what-is-an-assosiative-array-and-can-it-be-used-in-bash-part-1

In this mini-tutorial, we’ll be talking about how to access elements in an associative array. We’ll be accessing them individually with for loops.


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 11, 2021
Accepted Answer

Following the example from the previous part, we’ll create an Associative array called DOAssosArray and populate it with Counties as keys and Capitals as values:

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

As we said, we’ll be accessing elements of an associative array by using 2 different methods:

  • Access elements Individually
  • Access elements with a for loop

Access elements Individually

You can access values in an associative array by calling it’s key:

echo ${DOAssosArray[KEY]}

in our case

echo ${DOAssosArray[Germany]}

The output will be Berlin:

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

We can even ECHO all VALUES or KEYS in the array like so:

echo all VALUES:

root@nDevServer:~$ echo ${DOAssosArray[@]}
Madrid Berlin Paris

echo all KEYS:

root@nDevServer:~$ echo ${!DOAssosArray[@]}
Spain Germany France

Access elements with a for loop

Let’s say we need to print all values of an associative array, we’ll do that with a for loop.

for capitol in "${DOAssosArray[@]}"; do echo $capitol;done

Output:

root@DevServer:~$ for capitol in "${DOAssosArray[@]}"; do echo $capitol;done
Madrid
Berlin
Paris

Now, similar to the previous point, we can call all KEYS using the exclamation symbol (!) before the name of the array -

for country in "${!DOAssosArray[@]}"; do echo $country;done

Output:

root@DevServer:~$ for country in "${!DOAssosArray[@]}"; do echo $country;done
Spain
Germany
France

Next time we’ll be reviewing how to use WHILE loops with Associative arrays in BASH and what to use them for.

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