Reverse elements of an Array in Linux

In this blog, sharing code to reverse the array elements.

arr=(1 2 3 4) ## Array of 4 Elements
arr_2=() ## Another Array of zero Elements or you can say blank Array
len=${#arr[@]} ## It gives the length of an Array arr
while [ $len -gt “0” ]
do
len=$(( $len – 1))
arr_2+=(${arr[len]})
done
echo ${arr_2[*]} ##It will print (4 3 2 1)

Related posts