[Bash] Print an indexed array in reverse order

Created on November 30, 2026

For an array with consecutive indexes starting at 0, loop from the last index down to 0. This example prints the elements without changing their stored order.

fruits=(apple banana orange)

for ((i=${#fruits[@]}-1; i>=0; i--)); do
  printf '%s\n' "${fruits[i]}"
done
orange
banana
apple

The last index is one less than the number of elements in this example. If your indexes have gaps, use the actual indexes instead of assuming they run consecutively.

Bash Shell command