Bash

Associative Array (hash map)

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Bash 4+ supports key-value associative arrays. Must `declare -A` first — Bash won't infer it. Iterate keys with !arr[@].
Bash
Raw
declare -A users=(
    [alice]=admin
    [bob]=editor
    [carol]=viewer
)

# Access
echo "${users[alice]}"       # admin

# All keys / values
echo "${!users[@]}"          # alice bob carol  (order not guaranteed)
echo "${users[@]}"           # admin editor viewer

# Iterate
for u in "${!users[@]}"; do
    echo "$u -> ${users[$u]}"
done

# Add / remove
users[dave]=admin
unset 'users[bob]'

# Membership check
[[ -v users[alice] ]] && echo "alice exists"
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.