Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

how to reverse an array in bash scripting?

#Array declaration
array=(1 2 3 4 5 6 7) 
# Declaring index for updated reversed array
min=0
# Length of the array
max=$(( ${#array[@]} -1 ))
# Run while loop untill and unless min is less than max
while [[ min -lt max ]]
do
    # Swap current first and last elements
    x="${array[$min]}"
    array[$min]="${array[$max]}"
    array[$max]="$x"

    # Move closer
    (( min++, max-- ))
done
# Printing reverse array
echo "${array[@]}"
Source by unix.stackexchange.com #
 
PREVIOUS NEXT
Tagged: #reverse #array #bash
ADD COMMENT
Topic
Name
2+6 =