Indexed Arrays in Bash

From the Bash Scripting cheat sheet · Arrays · verified Jul 2026

Indexed Arrays

Ordered, zero-based lists.

bash
# Quick Reference
arr=(a b c)
echo "${arr[0]}"      # a
echo "${arr[@]}"      # all elements
echo "${#arr[@]}"     # length -> 3
arr+=(d)              # append
💡 ${arr[@]} expands to all elements; ${#arr[@]} is the count.
⚡ Always quote "${arr[@]}" in loops so elements with spaces stay intact.
📌 Arrays are zero-indexed; ${arr[-1]} grabs the last element (bash 4.3+).
🟢 arr+=(item) appends; ${!arr[@]} lists the indices (useful for sparse arrays).
arraysindexed

More Bash tasks

Back to the full Bash Scripting cheat sheet