Open-Source Bash Programming • Intermediate • Estimated study time: 30 minutes.
Infrastructure scripts frequently process lists of devices and associate each identifier with a configuration value. A single space-delimited string cannot reliably preserve a list of names containing spaces. Arrays solve that representation problem. After studying pipeline status and error handling, learners can organize the inputs and outputs of those automation workflows using native Bash collections.
Prerequisites and learning objectives
Use Bash 4 or later in a local terminal. Bash 3 lacks associative arrays; check the interpreter version with bash –version. Review variables, expansion and quoting. Learners will declare and append indexed arrays, use string-keyed associative arrays, preserve argument boundaries, identify sparse indices and handle missing keys.
Indexed and associative arrays
Indexed arrays use integer subscripts, starting at zero for ordinary sequential assignments. Associative arrays use nonempty string keys and must be declared with declare -A. The GNU Bash array documentation describes both forms. Quoted expansion with “${array[@]}” produces a separate shell argument for each element, retaining spaces inside individual values. Quoted “${array[*]}” instead joins the elements into one argument using the first character of IFS. Unquoted expansions can introduce word splitting and pathname expansion.
Dave Eddy demonstrates associative arrays in the following original Bash programming course video:
Laboratory: simulated rack inventory
Save the following script as inventory.sh in a disposable working directory. It reads no production devices and makes no network requests:
#!/usr/bin/env bash
racks=('Rack A' 'Rack B' 'Rack C')
racks+=('Rack D')
declare -A status=(["Rack A"]=ready ["Rack B"]=hold ["Rack C"]=ready)
for rack in "${racks[@]}"; do
printf '%s: %s\n' "$rack" "${status[$rack]-unknown}"
done
printf 'Count: %s\n' "${#racks[@]}"
unset 'racks[1]'
for index in "${!racks[@]}"; do
printf '%s=%s\n' "$index" "${racks[$index]}"
done
Run the syntax check and execute the script using the following commands:
bash -n inventory.sh
bash inventory.sh
The verified output under GNU Bash 5.2.37 is:
Rack A: ready
Rack B: hold
Rack C: ready
Rack D: unknown
Count: 4
0=Rack A
2=Rack C
3=Rack D
The indexed array defines traversal order. The associative array stores a simulated status for three rack identifiers. The missing Rack D entry receives an explicit fallback through parameter expansion. The single hyphen substitutes unknown only when the element is unset; an existing empty value remains empty. A colon before the hyphen would also substitute for an empty value.
Sparse indices and iteration
Removing index 1 does not renumber the remaining elements. The length expansion reports the number of assigned elements, not the largest index plus one. Use “${!racks[@]}” to retrieve assigned indices instead of assuming every index below the length exists. The earlier Bash loops lesson explains the for construct used here. Associative arrays have no guaranteed insertion order; traverse an independent ordered list when reports require predictable ordering.
The following supplementary video explores key-value dictionaries in Bash:
Exercises and answer key
1. Add Rack E without a status. 2. Predict the indices after removing index 0 as well. 3. Explain why quoting the array expansion matters. 4. Assign an empty status to Rack D and predict the resulting output.
Answers: 1. Append Rack E to racks; its status is unknown. 2. Indices 2 and 3 remain in the original example. 3. Quoting preserves element boundaries and prevents glob expansion. 4. Rack D receives an empty displayed status rather than the unset-key fallback. Use the colon-hyphen form when both missing and empty values require a fallback.
Conclusion
Arrays preserve structured arguments, while associative arrays provide efficient key-based lookup for shell scripts. Combine predictable traversal with explicit missing-value handling, and consult Bash functions and scope before incorporating array processing into reusable automation.
We volunteer daily to ensure the credibility of the information on the platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiative, please scan the QR code below.


Leave a Reply