[Code Example] Reverse an array using bash script

Total
0
Shares

In this article we will provide you bash script code to reverse an array. In this function you can provide an array or multiple arguments.

Code Example –

#!/bin/bash

reverse_array() {
    shopt -s extdebug
    f()(echo "${BASH_ARGV[@]}"); f "[email protected]"
    shopt -u extdebug
}



arr=("Animals" "are" "like" "humans")

reverse_array "${arr[@]}"

# Output:
# humans like are Animals

Inspired from Dylan Araps

Live Demo

Demo might not work in private window (incognito)

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like