Indexed Arrays
Bash arrays are the right tool when one variable needs to preserve several distinct shell values. Indexed arrays keep element boundaries intact, making them far safer than space-delimited strings for service names, paths, command arguments, and other small in-memory collections.
Learning objectives
By the end of this lesson
- Create and inspect indexed arrays.
- Append, replace, slice, and delete elements.
- Iterate without losing whitespace.
- Understand sparse numeric indexes.
- Use arrays to construct commands safely.
1. Create arrays explicitly
An indexed array stores values under integer indexes. Bash can infer array creation from indexed assignment, but declare -a makes intent explicit.
declare -a services
services[0]="api"
services[1]="worker"
services[2]="cache"
printf 'first=%s\n' "${services[0]}"
printf 'second=%s\n' "${services[1]}"2. Array literals preserve element boundaries
services=("api gateway" "worker" "cache*")
printf 'count=%d\n' "${#services[@]}"
for service in "${services[@]}"; do
printf 'service=<%s>\n' "$service"
doneThe string "api gateway worker" does not encode reliable element boundaries. An array does.
3. Append with +=
services=("api" "worker")
services+=("cache")
services+=("metrics" "scheduler")
printf '%s\n' "${services[@]}"Appending multiple elements in one operation is convenient when optional CLI arguments or discovered resources need to extend an existing list.
4. Array indexes can be sparse
declare -a ports
ports[0]=8080
ports[5]=9090
printf 'indexes: %s\n' "${!ports[@]}"
printf 'values: %s\n' "${ports[@]}"
printf 'count: %d\n' "${#ports[@]}"The element count is not necessarily equal to the highest index plus one. Do not assume dense numbering unless your own code maintains it.
5. Replace elements by index
images=("api:v1" "worker:v1" "cache:v1")
images[1]="worker:v2"
printf '%s\n' "${images[@]}"6. unset removes elements
services=("api" "worker" "cache")
unset 'services[1]'
printf 'indexes=%s\n' "${!services[@]}"
printf 'values=%s\n' "${services[@]}"Removing one element can leave a gap. If you need a dense list again, rebuild it or assign from a filtered expansion.
7. Slice arrays without converting them to strings
items=(zero one two three four five)
printf 'from index 2: %s\n' "${items[@]:2}"
printf 'three items: %s\n' "${items[@]:1:3}"Quoted array slicing preserves one shell word per selected element.
8. Distinguish array length from element string length
items=("api" "worker service" "cache")
printf 'array elements=%d\n' "${#items[@]}"
printf 'length of element 1=%d\n' "${#items[1]}"9. Arrays are ideal for command construction
curl_args=(
--fail
--silent
--show-error
--connect-timeout 5
)
[[ $verbose == true ]] && curl_args+=(--verbose)
curl "${curl_args[@]}" "$url"Each array element becomes one command argument. Spaces and wildcard characters remain data rather than becoming new shell syntax.
10. Copy arrays with quoted expansion
original=("api gateway" "worker" "cache")
copy=("${original[@]}")
copy+=("metrics")
printf 'original=%d copy=%d\n' \
"${#original[@]}" "${#copy[@]}"11. Hands-on lab: deployment target array
mkdir -p "$HOME/devops-academy/bash/chapter07/lesson01"
cd "$HOME/devops-academy/bash/chapter07/lesson01"
targets=("api gateway" "worker" "cache")
targets+=("metrics")
printf 'target count=%d\n' "${#targets[@]}"
for target in "${targets[@]}"; do
printf 'PLAN target=<%s>\n' "$target"
done
unset 'targets[2]'
printf 'remaining indexes: %s\n' "${!targets[@]}"
printf 'remaining values:\n'
printf ' <%s>\n' "${targets[@]}"Verification checklist
12. Knowledge check
Question 1. What does "${array[@]}" produce?
Question 2. Can indexed arrays have gaps?
Question 3. What does ${#array[@]} mean?
Question 4. Why are arrays good for constructing commands?
13. Summary
Indexed arrays are Bash's safest in-memory representation for small ordered collections. Use them for arguments, paths, service names, and similar values where exact element boundaries matter.
14. Further reading
- GNU Bash Reference Manual — Arrays.
- GNU Bash Reference Manual — Shell Parameter Expansion.
- GNU Bash Reference Manual — Compound Assignments.
- ShellCheck documentation — array expansion and quoting.
Keep the academy open
Support free, practical DevOps education.
Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0
Send only Ethereum/ERC-20 compatible assets to this address.