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.
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: SeattleIterating 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]}"
doneOutput:
name: Alice
age: 28
city: SeattleRemoving 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]}"
doneOutput after removal:
name: Alice
city: SeattleChecking 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."
fiRunning 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]}"
doneOutput:
this: 2
is: 2
a: 2
test: 2
only: 1Conclusion
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
