Fundamentals 6 min read

Master Bash Associative Arrays: Declaration, Access, Traversal, and Real-World Uses

This guide explains how Bash 4+ associative arrays work, covering declaration, initialization, element access, iteration, element removal, key existence checks, and practical examples such as a word‑frequency counter, enabling more flexible and efficient shell scripting.

Ops Development & AI Practice
Ops Development & AI Practice
Ops Development & AI Practice
Master Bash Associative Arrays: Declaration, Access, Traversal, and Real-World Uses

Introduction

In Bash scripting, arrays are common, but traditional indexed arrays lack flexibility. Bash 4.0 and later introduce associative arrays, allowing string keys similar to maps or dictionaries in other languages. This article details their usage and practical applications.

Declaring and Initializing an Associative Array

Use the declare -A keyword to create an associative array. The example below declares myMap and adds three key‑value pairs.

#!/bin/bash
# Declare an associative array
declare -A myMap

# Add key‑value pairs
myMap["name"]="Alice"
myMap["age"]="28"
myMap["city"]="Seattle"

The script creates myMap with keys name, age, and city, each storing a string value.

Accessing Elements

Elements are accessed using their string keys, just like indexed arrays but with the key name.

#!/bin/bash
declare -A myMap
myMap["name"]="Alice"
myMap["age"]="28"
myMap["city"]="Seattle"

echo "Name: ${myMap["name"]}"
echo "Age: ${myMap["age"]}"
echo "City: ${myMap["city"]}"

Running the script outputs:

Name: Alice
Age: 28
City: Seattle

Iterating Over an Associative Array

The for loop can iterate over all keys using ${!myMap[@]}, then retrieve each value.

#!/bin/bash
declare -A myMap
myMap["name"]="Alice"
myMap["age"]="28"
myMap["city"]="Seattle"

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

Output:

name: Alice
age: 28
city: Seattle

Removing Elements

The unset command deletes a specific key from the array.

#!/bin/bash
declare -A myMap
myMap["name"]="Alice"
myMap["age"]="28"
myMap["city"]="Seattle"

unset myMap["age"]

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

Output after removal:

name: Alice
city: Seattle

Checking for a Key

Use an if statement with the -v test to verify a key’s existence.

#!/bin/bash
declare -A myMap
myMap["name"]="Alice"
myMap["age"]="28"
myMap["city"]="Seattle"

if [[ -v myMap["age"] ]]; then
    echo "Age exists in the array."
else
    echo "Age does not exist in the array."
fi

Running the script prints:

Age exists in the array.

Practical Use: Word‑Frequency Counter

Associative arrays are handy for counting occurrences, such as word frequencies in a string.

#!/bin/bash
declare -A wordCount

text="this is a test this is only a test"

for word in $text; do
    ((wordCount[$word]++))
 done

for word in "${!wordCount[@]}"; do
    echo "$word: ${wordCount[$word]}"
 done

Output:

this: 2
is: 2
a: 2
test: 2
only: 1

Conclusion

Associative arrays greatly enhance Bash scripting by providing flexible key‑value storage, enabling more readable and efficient scripts for configuration handling, JSON parsing, word‑frequency analysis, and other tasks. Mastering declaration, access, iteration, deletion, and key checks empowers developers to write better shell code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

scriptingbashassociative arraybash-arraysshell-scripting
Ops Development & AI Practice
Written by

Ops Development & AI Practice

DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.