0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Reverse an Array with Example

Use a loop to iterate backward over the array indices and print elements in reverse order, like for (( i=${#arr[@]}-1; i>=0; i-- )); do echo "${arr[i]}"; done.
📋

Examples

Inputarr=(1 2 3 4 5)
Output5 4 3 2 1
Inputarr=(apple banana cherry)
Outputcherry banana apple
Inputarr=()
Output
🧠

How to Think About It

To reverse an array in Bash, think about accessing elements starting from the last index down to the first. You loop backward over the array indices and print or store each element in that order.
📐

Algorithm

1
Get the array and find its length.
2
Start a loop from the last index to the first index.
3
In each loop iteration, access the element at the current index.
4
Print or save the element to build the reversed array.
5
End the loop and output the reversed array.
💻

Code

bash
arr=(apple banana cherry)
for (( i=${#arr[@]}-1; i>=0; i-- )); do
  echo "${arr[i]}"
done
Output
cherry banana apple
🔍

Dry Run

Let's trace reversing arr=(apple banana cherry) through the code

1

Initialize array and length

arr=(apple banana cherry), length = 3

2

Loop from index 2 to 0

i=2: print arr[2] = cherry

3

Next iterations

i=1: print arr[1] = banana i=0: print arr[0] = apple

Index iElement arr[i]
2cherry
1banana
0apple
💡

Why This Works

Step 1: Access array length

We use ${#arr[@]} to get the number of elements in the array.

Step 2: Loop backward

The loop starts from the last index length-1 and decrements to 0 to reverse the order.

Step 3: Print elements

Each element is accessed by ${arr[i]} and printed, producing the reversed output.

🔄

Alternative Approaches

Using a temporary array to store reversed elements
bash
arr=(1 2 3 4 5)
rev=()
for (( i=${#arr[@]}-1; i>=0; i-- )); do
  rev+=("${arr[i]}")
done
printf "%s\n" "${rev[@]}"
This method creates a new array for reversed elements, useful if you want to keep the reversed array for later use.
Using Bash parameter expansion with a loop
bash
arr=(a b c d)
for ((i=0; i<${#arr[@]}; i++)); do
  echo "${arr[$((${#arr[@]}-1-i))]}"
done
This loops forward but calculates the reversed index inside the loop, which is another way to reverse.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs once for each element, so time grows linearly with array size.

Space Complexity

Reversing in place by printing uses constant extra space; creating a new array uses O(n) space.

Which Approach is Fastest?

Printing elements directly backward is fastest and uses least memory; building a new array is useful if reversed data is needed later.

ApproachTimeSpaceBest For
Loop backward and printO(n)O(1)Quick output without extra storage
Build new reversed arrayO(n)O(n)When reversed array is needed later
Loop forward with reversed indexO(n)O(1)Alternative indexing method
💡
Use ${#arr[@]} to get array length and loop backward to reverse easily.
⚠️
Beginners often try to reverse arrays by looping forward without adjusting indices, which does not reverse the order.