Key-Value Lookups With Associative Arrays
Map names to values in bash without spawning a lookup tool.
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
!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
- bash.1.en.html manual pageEvidence: The bash manual page documents associative arrays.
Last verified: 2026-10-13 · Category: Developer