Arrays & Associative Maps

Indexed Arrays

An indexed array maps integer subscripts to strings. Subscripts start at 0 and do not have to be contiguous.

Creating an Array

Compound assignment with parentheses builds an indexed array in a single expression, while += appends new elements without overwriting existing indices. Because Bash arrays are sparse, you can also assign to any non-negative integer index directly without preallocating intermediate slots.

fruits=(apple banana cherry)         # three elements, indices 0..2
fruits+=(date)                      # append one element
declare -a caps                     # explicit declaration
caps[0]=A; caps[2]=C                # sparse: index 1 is unset
fruits=(apple banana cherry) is three elements. fruits=("apple banana cherry") is one element. Spaces separate; quotes bind.

Accessing Elements

Parameter expansion syntax lets you read individual elements, count populated slots, or inspect which indices are currently defined. Notice the difference between @ and *: when double-quoted, "${fruits[@]}" preserves each element as its own argument, whereas "${fruits[*]}" joins them with the first character of IFS.

echo "${fruits[0]}"      # apple
echo "${fruits[-1]}"     # date   (last element, Bash 4.3+)
echo "${fruits[@]}"      # all elements as separate words
echo "${fruits[*]}"      # all elements joined into one word
echo "${#fruits[@]}"     # number of elements
echo "${!fruits[@]}"     # the indices themselves

Always quote "${arr[@]}". Unquoted ${arr[@]} re-splits each element on spaces, exactly like a bare $var.

Slicing

Range slicing extracts a contiguous window of elements using ${arr[@]:offset:length}, which is useful when skipping positional flags or batching arguments. The same syntax works on the positional parameters array $@.

set -- one two three four
echo "${@:2:2}"                    # two three  (2 elements from index 2)
a=(10 20 30 40 50)
echo "${a[@]:1:3}"                 # 20 30 40

Iterating

Looping over "${fruits[@]}" visits every value safely even when filenames or strings contain whitespace. When you need the index alongside the value—for example, when updating elements in place or handling sparse arrays—iterate over "${!fruits[@]}" instead.

for f in "${fruits[@]}"; do
  printf 'item: %s\n' "$f"        # one iteration per element, spaces safe
done

for i in "${!fruits[@]}"; do
  printf '%d -> %s\n' "$i" "${fruits[i]}"
done

Modifying and Removing

You can overwrite an element by index, delete a single slot with unset, or clear the entire array by assigning (). Always quote the subscript passed to unset so Bash does not attempt pathname globbing if a file matching the brackets exists in the working directory.

fruits[1]="blueberry"    # replace
unset 'fruits[2]'        # delete index 2 (quote it! otherwise glob risk)
fruits=()                # empty the whole array

Associative Arrays

Declared with declare -A, associative arrays use arbitrary string keys. They are the closest thing Bash has to a dictionary.

Declaring and Filling

Associative arrays require Bash 4.0 or newer and must be declared explicitly with declare -A (or local -A inside a function). You can populate keys individually or batch multiple key-value pairs using bracketed compound syntax.

declare -A score
score[alice]=90
score[bob]=75
score+=( [carol]=88 [dave]=64 )     # add several at once
You must declare with declare -A before the first assignment. Without it Bash creates an indexed array and score[alice] silently evaluates to index 0.

Reading and Iterating

Lookups use the string key inside brackets, and "${!score[@]}" expands to the set of keys. Because hash-table iteration order is unspecified in Bash, avoid relying on insertion order when printing reports or comparing output.

echo "${score[alice]}"        # 90
echo "${#score[@]}"           # 4
for k in "${!score[@]}"; do
  printf '%-6s %s\n' "$k" "${score[$k]}"
done

A Practical Lookup Table

Associative arrays shine as lightweight configuration or dispatch tables in shell scripts, replacing repetitive case blocks when mapping extensions, environments, or service endpoints. Combining lookup expansion with a default fallback (:-unknown) keeps scripts resilient under set -u.

declare -A ext=(
  [sh]="shell"   [py]="python"
  [js]="javascript"  [bash]="shell"
)

describe() {
  local f="$1" e="${1##*.}"
  printf '%s -> %s\n' "$f" "${ext[$e]:-unknown}"
}
describe notes.py       # notes.py -> python
describe run.sh         # run.sh -> shell

Pitfalls

Array bugs in Bash are often silent because unquoted expansions and missing declarations rarely raise syntax errors immediately. Keep these common traps in mind when writing portable scripts:

  • Unquoted [@]: splits elements on spaces — always "${arr[@]}".
  • Bash 3.2 on macOS: no associative arrays and no negative indices. Install a newer Bash or avoid those features.
  • Passing arrays to functions: a function cannot see your array by name across a subshell; pass "${arr[@]}" as arguments and rebuild with local -a copy=("$@").
  • Sparse arrays: ${#a[@]} counts set elements, not the highest index + 1.