Associative Arrays in Bash

From the Bash Scripting cheat sheet · Arrays · verified Jul 2026

Associative Arrays

Key-value maps (bash 4+).

bash
# Quick Reference
declare -A ages
ages[sam]=30
echo "${ages[sam]}"       # 30
echo "${!ages[@]}"        # keys
💡 Associative arrays require declare -A first - otherwise keys are treated as 0.
⚡ ${!map[@]} gives keys; ${map[@]} gives values; ${#map[@]} gives the count.
📌 Associative arrays need bash 4+ (macOS ships bash 3.2 - install a newer bash).
🟢 [[ -v map[key] ]] tests whether a key exists without inserting it.
arraysassociative

More Bash tasks

Back to the full Bash Scripting cheat sheet