Search
 
SCRIPT & CODE EXAMPLE
 

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

PREVIOUS NEXT
Code Example
Shell :: git add all tracked files 
Shell :: install discord on ubuntu 
Shell :: ubuntu server scan virus 
Shell :: crontab file location 
Shell :: restart network centos 
Shell :: git untrack file 
Shell :: homebrew restart redis 
Shell :: heroku cli connect to app 
Shell :: Could not install from "HussainAppDataRoaming pm-cache\_npx15208" as it does not contain a package.json file. 
Shell :: increment number bash 
Shell :: install brave browser on linux 
Shell :: how to reload bashrc 
Shell :: kill processes on port 80 
Shell :: how to find and replace a string in a file using shell script 
Shell :: Install dia for linux ubuntu 
Shell :: get docker script 
Shell :: update font cache linux 
Shell :: There are no commands defined in the "ide-helper" namespace 
Shell :: flutter errors shader compilation error 
Shell :: xampp the installer requires root privileges 
Shell :: screen kill session 
Shell :: enable epel repo centos 7 
Shell :: install wine ubuntu 20.04 
Shell :: certbot add new domain 
Shell :: tar a folder with subfolders 
Shell :: dev/kvm device permission denied 
Shell :: install waitress 
Shell :: brew Failure while executing; `/bin/launchctl bootstrap gui/501 /Users/webhasan/Library/LaunchAgents/homebrew.mxcl.httpd.plist` exited with 5. 
Shell :: download spotify ubuntu 
Shell :: remote: HTTP Basic: Access denied fatal: Authentication failed for 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =