Practice Exercise in Bash Basics Series #5: Using Arrays in Bash

If you are following the Bash Basics series on It’s FOSS, you can submit and discuss the answers to the exercise at the end of the chapter:

Fellow experienced members are encouraged to provide their feedback to new members.

Do note that there could be more than one answer to a given problem.

#!/bin/bash

numbers=()

read -p "Enter 3 numbers: " num

IFS=' ' read -r -a numbers <<< "$num"

numbers_length=${#numbers[@]}

reverse_numbers=()

for (( i=$numbers_length-1; i>=0; i-- )); do
        reverse_numbers+=("${numbers[i]}")
done

echo "Numbers in reverse order are: ${reverse_numbers[*]}"

I liked the use of IFS. I guess I deliberately omitted that from the tutorial.