HOWTO · Bash
Bash Associative Array
This tutorial demonstrates how to create an associative array in Bash.
On this page
This guide shows how to create and use a Bash associative array: map string keys to values, look up entries, iterate over keys and values, add or update entries, and remove a key. The examples are standalone snippets for you to run in a Bash shell.
Check Bash Support and Understand Associative Arrays
An associative array maps string keys to values. Unlike an indexed array, its elements are selected by names such as One rather than numeric positions. Bash’s declare builtin creates the array. To inspect the active Bash version, run:
bash --version
This command reports version information; that information alone does not verify feature support in a particular shell context. Run the examples in the Bash environment where the script will be used.
Declare an Associative Array
Use declare -A to create an associative array before assigning keys. Quote values that contain spaces, and quote expansions when passing array contents to commands so each value remains a single argument.
Run this example:
declare -A DemoArray
DemoArray[One]="Delftstack1"
DemoArray[Two]="Delftstack2"
printf '%s\n' "${DemoArray[One]}" "${DemoArray[Two]}"
You can also initialize keys and values in the declaration. Keep each key-value assignment inside the parentheses:
declare -A DemoArray1=( [One]="Delftstack1" [Two]="Delftstack2" [Three]="Delftstack3" )
printf '%s\n' "${DemoArray1[One]}" "${DemoArray1[Two]}" "${DemoArray1[Three]}"
Read Keys and Values
Use ${DemoArray[One]} to retrieve the value associated with one key. The [@] expansion selects all values, while ${!DemoArray[@]} selects all keys. Quoting these expansions preserves values containing whitespace as separate items in a loop. A key-value loop can look up each current key to print its associated value:
declare -A DemoArray=( [One]="Delftstack1" [Two]="Delftstack2" [Three]="Delftstack3" )
printf 'One: %s\n' "${DemoArray[One]}"
for key in "${!DemoArray[@]}"; do
printf 'key: %s\n' "$key"
done
for value in "${DemoArray[@]}"; do
printf 'value: %s\n' "$value"
done
for key in "${!DemoArray[@]}"; do
printf '%s => %s\n' "$key" "${DemoArray[$key]}"
done
Associative-array iteration order is unspecified. Do not rely on keys or values appearing in the same order across runs; use each key to retrieve the matching value instead of pairing separate key and value lists by position. If a program needs a stable presentation order, sort the keys separately before printing them.
Add or Update Entries
Assigning to a key adds it when it is new and replaces the value when that key already exists. The += compound assignment can add one or more key-value pairs to an existing associative array. The example demonstrates both operations; the displayed sequence is not a promise about iteration order:
declare -A DemoArray=( [One]="Delftstack1" [Two]="Delftstack2" )
DemoArray+=([Three]="Delftstack3")
DemoArray[Two]="Updated value"
for key in "${!DemoArray[@]}"; do
printf '%s => %s\n' "$key" "${DemoArray[$key]}"
done
Use a quoted value when it contains spaces. Assigning a value to an existing key does not create a second copy of that key; it changes the value associated with it.
Remove Entries
Pass the array element to unset to remove one key and its value. Quote the subscript expression so the shell passes it to the builtin as a single argument. Other entries remain available, and looking up a removed key no longer retrieves its former value.
declare -A DemoArray=( [One]="Delftstack1" [Two]="Delftstack2" [Three]="Delftstack3" )
unset 'DemoArray[Two]'
for key in "${!DemoArray[@]}"; do
printf '%s => %s\n' "$key" "${DemoArray[$key]}"
done
Iterate over the remaining keys to inspect the array after removal. Since their order is unspecified, check for a particular key by name when the next operation depends on its presence.