Skip to content
Linux Basics: Store Related Values in Bash Arrays

Linux Basics: Store Related Values in Bash Arrays

October 18, 2026

A list of filenames should not become one fragile string. Bash arrays hold separate values so spaces can stay part of each item. Practice lists, sparse indexes, and named lookups without touching existing files.

These examples were tested on Linux with GNU Bash 5.2.37. Use Bash rather than sh; some syntax is Bash-specific. No packages or administrator access are needed.

Prepare a scratch directory

You need Bash, a text editor, and the mktemp, rm, and rmdir utilities. Keep using the same terminal. The guards stop the shell if setup fails. Save each named source file in this directory before running its command. Calling bash explicitly means you do not need to change executable permissions.

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

1. Keep words together

Save this as array-items.sh:

#!/usr/bin/env bash
items=('red apple' 'green pear')
items+=('blue berry')
printf 'Count: %s\n' "${#items[@]}"
printf 'Item: <%s>\n' "${items[@]}"

Run it:

bash array-items.sh

Output:

Count: 3
Item: <red apple>
Item: <green pear>
Item: <blue berry>

An indexed array starts at index zero. += appends an element. Quoted "${items[@]}" supplies one argument per element, preserving spaces. Do not replace it with an unquoted expansion or a single string of space-separated names.

2. Inspect sparse indexes

Save this as array-indexes.sh:

#!/usr/bin/env bash
tasks=([0]='read notes' [3]='write summary')
unset 'tasks[0]'
tasks[5]='send review'
printf 'Count: %s\n' "${#tasks[@]}"
for index in "${!tasks[@]}"; do
  printf '%s: %s\n' "$index" "${tasks[index]}"
done

Run it:

bash array-indexes.sh

Output:

Count: 2
3: write summary
5: send review

Removing an element does not renumber the remaining indexes. ${#tasks[@]} counts assigned elements, while ${!tasks[@]} lists their indexes. Quote the argument to unset so the shell cannot treat its brackets as a filename pattern.

3. Look up values by name

Save this as array-lookup.sh:

#!/usr/bin/env bash
declare -A ports=([web]=8080 [metrics]=9090)
ports[admin]=8081
for service in web metrics admin; do
  printf '%s: %s\n' "$service" "${ports[$service]}"
done
printf 'Missing: %s\n' "${ports[unknown]:-not configured}"

Run it:

bash array-lookup.sh

Output:

web: 8080
metrics: 9090
admin: 8081
Missing: not configured

An associative array uses string keys and requires declare -A. It needs Bash 4 or newer. The explicit service list gives deterministic output; do not depend on associative-array iteration order. :- supplies a fallback for a missing or empty value.

Clean up

In the same terminal, remove only the example files, return to your original directory, and remove the empty scratch directory:

rm -- array-items.sh array-indexes.sh array-lookup.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

Reference

The GNU Bash manual: Arrays documents the syntax used here.

Acknowledgements

This post was created with assistance from AI.

Last updated on