Key-Value Lookups With Associative Arrays

Map names to values in bash without spawning a lookup tool.

macOSbashshellscriptingLow risk
Run declare -A sizes=([small]=1 [large]=3) then read ${sizes[large]}.

01Command

Command
$ declare -A sizes=([small]=1 [large]=3); echo "${sizes[large]}"

02What it does

Associative arrays turn a chain of conditionals into a direct lookup, which makes configuration tables in scripts short and readable. Requires bash 4+, so macOS users may need Homebrew bash; this is pure scripting capability with no GUI counterpart.

03Risk

RiskLow risk
RequirementsmacOS default zsh/bash (bash 3.2+; some features need bash 4+ via Homebrew)
CompatibilitymacOS with bash 3.2+ (GNU bash 4+ via Homebrew for advanced features)
!Warning: macOS ships bash 3.2, which lacks associative arrays; install bash 4+ or use zsh's typeset -A.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

Last verified: 2026-10-13 · Category: Developer