Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

reverse an array in shell 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 #shell #scripting
ADD COMMENT
Topic
Name
3+9 =