Search
 
SCRIPT & CODE EXAMPLE
 

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

PREVIOUS NEXT
Code Example
Shell :: wget clone entire website 
Shell :: git setup 
Shell :: table markdown github 
Shell :: view git stash without applying 
Shell :: linux download youtube mp3 
Shell :: linux decode base64 terminal 
Shell :: .gitignore file not ignoring the file 
Shell :: git updates were rejected because the tip of your current branch is behind 
Shell :: linux screen brightness command line 
Shell :: ufw see open ports 
Shell :: kubectl scale deployment 
Shell :: reload bashrc 
Shell :: docker remove all images 
Shell :: firebase commands 
Shell :: powershell print to console 
Shell :: docker install script 
Shell :: how to install brave on fedora 
Shell :: depends: libssl1.1 (= 1.1.1) but it is not installable 
Shell :: flutter skia shader compilation error 
Shell :: cf rolling restart 
Shell :: how to remove all docker container at once 
Shell :: git revert last commit remote 
Shell :: gyp error npm install 
Shell :: certbot add new domain pache 
Shell :: install jupyter notebook ubuntu 20.04 
Shell :: delete vscode mac 
Shell :: grep docker logs 
Shell :: corntab using nano 
Shell :: install django jazzmin 
Shell :: install aws cli in ec2 centos 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =