dylanaraps / pure-bash-bible

📖 A collection of pure bash alternatives to external processes.
MIT License
36.41k stars 3.27k forks source link

Much More Compatible `reverse_array()` #119

Open terminalforlife opened 2 years ago

terminalforlife commented 2 years ago

https://github.com/dylanaraps/pure-bash-bible#reverse-an-array

This approach is not restricted to >= 5.0, which will probably be a huge deal to some people looking to write more portably. It works in >= 3.1. Tested in all full releases of BASH from 3.0 to 5.1.8.

reverse_array() {
    local Buffer=("$@")
    for (( Index = $# - 1; Index >= 0; Index-- )); {
        printf '%s\n' "${Buffer[Index]}"
    }
}

The only reason it didn't work in BASH 3.0 and 3.0.16 was because of the local builtin — perhaps an old bug or limitation later addressed.