Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

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[@]}"
Comment

PREVIOUS NEXT
Code Example
Shell :: install perl linux 
Shell :: poetry to requirements.txt 
Shell :: how to change a commit message after push 
Shell :: rick roll terminal command 
Shell :: remove home icon ubuntu 
Shell :: mongodb shell query last document 
Shell :: bash if element in array 
Shell :: how to install flask_sqlalchemy 
Shell :: ubuntu remove package and dependencies install deb 
Shell :: random string linux 
Shell :: ln phpmyadmin 
Shell :: restart bashrc 
Shell :: kill process running on port in windows 
Shell :: how to know my ros version 
Shell :: linux ram info 
Shell :: install next globally 
Shell :: node in ubuntu 
Shell :: install supervisor di linux 
Shell :: shell get size of directory 
Shell :: docker runlike 
Shell :: how to kill linux screen 
Shell :: List User Accounts Mac 
Shell :: powershell keep skype active 
Shell :: certbot apache site 
Shell :: voice recorder for ubuntu 20.04 
Shell :: how to install arduino ide on ubuntu 
Shell :: Command `npm install --save --save-exact react-native` failed 
Shell :: install nginx amazon linux 2 
Shell :: upgrade beautifulsoup version 
Shell :: ImportError: cannot import name docevents 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =