
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: August 4, 2024
Bash is one of the most commonly used command-line interpreters in Linux. One of the data structures in Bash is the array, which allows us to store a sequence of elements. When working with an array, it’s not uncommon to encounter a scenario that requires us to reverse the array.
In this tutorial, we’ll explore various ways to reverse a Bash array.
In Bash, we can identify each element of an array with the help of its index. Thus, indices might come in handy when we’re reversing the array.
To demonstrate, we’ll use an array with the name numbers_array:
$ numbers_array=(1 2 3 4)
Now, let’s explore various ways to reverse this array. In addition, we’ll utilize the Bash script example.sh.
In this approach, we’ll manually swap array elements in place so that we’re not required to create an additional array:
#!/bin/bash
# Initialize the original array
numbers_array=(1 2 3 4)
# Reverse the array in place
length=${#numbers_array[@]}
for (( i=0, j=length-1; i<j; i++, j-- )); do
temp="${numbers_array[i]}"
numbers_array[i]="${numbers_array[j]}"
numbers_array[j]="$temp"
done
# Print the reversed array
echo "Reversed Array: ${numbers_array[@]}"
Let’s break down the command above:
Now, let’s see the output of the Bash script example.sh:
$ bash example.sh
Reversed Array: 4 3 2 1
This approach exchanges elements in the array numbers_array from both ends toward the center.
Typically, the tac command helps to print files in reverse. In addition, we can use it to reverse the numbers_array array:
$ reversed_numbers=($(printf "%s\n" "${numbers_array[@]}" | tac))
Now, let’s explore the commands we use with command substitution ($()):
Meanwhile, reversed_numbers=($(…)) captures the output of the command inside $() and stores it in the reversed_numbers array.
At this point, let’s display the contents of the reversed_numbers array:
$ echo "Reversed array: ${reversed_numbers[@]}"
Reversed array: 4 3 2 1
Here, we see that the array is reversed.
Here, we iterate over the original array numbers from the last to the first element and append each element to a new array.
So, let’s use a for loop to iterate over the original array in reverse order and add each element to the reversed_numbers array:
#!/bin/bash
# Initialize the original array
numbers_array=(1 2 3 4)
# Create a new array to hold the reversed elements
reversed_numbers=()
# Get the length of the array
length=${#numbers_array[@]}
# Inversely iterate through the array
for ((i=$length-1; i>=0; i--)); do
reversed_numbers+=(${numbers_array[i]})
done
# Print the reversed array
echo ${reversed_numbers[@]}
Let’s discuss the command above:
Now, let’s execute example.sh:
$ bash example.sh
4 3 2 1
Here, we reverse the elements of the numbers_array array, store them in the reversed_numbers array, and then print the reversed_numbers array.
Another option that we can utilize is using a recursive function to reverse the array. To demonstrate, we’ll declare the function inside a shell script:
#!/bin/bash
# Initialize the original array
numbers_array=(1 2 3 4)
# Define a recursive function to reverse the array
reverse_array() {
local array=("$@")
local length=${#array[@]}
if (( length > 0 )); then
echo "${array[-1]}"
reverse_array "${array[@]::length-1}"
fi
}
# Capture the reversed elements
reversed_array=($(reverse_array "${numbers_array[@]}"))
# Print the original and reversed arrays
echo "Original Array: ${numbers_array[@]}"
echo "Reversed Array: ${reversed_array[@]}"
So, let’s describe the recursive function reverse array:
Now, let’s execute example.sh:
$ bash example.sh
Original Array: 1 2 3 4
Reversed Array: 4 3 2 1
Importantly, reversed_array=($(reverse_array “${numbers_array[@]}”)) executes the reverse_array function with numbers_array as its input and stores the output into reversed_array.
In this article, we explored approaches that we can utilize to reverse a Bash array.
Methods include using the tac command, implementing a for loop, employing a recursive function, and manually swapping array elements. So, understanding these methods increases our ability to perform Bash scripting. Additionally, we have the option to choose between the different techniques based on preference or use case.