Shell Script To Reverse Command Line Input / Numbers

#!/bin/bash
# Write a shell program that outputs all integers upto the command line
# parameter starting from 1 and also should output the same numbers 
# in the reverse order.
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft project.
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
if [ $# -eq 0 ]
then
	echo "$0 num1, num2, numN"
	exit 1
fi
x=""
echo -n "Numbers are : " 
for n in $@
do
  echo -n $n
  echo -n " "
  x="$n $x"
done
echo ""
echo -n "Reverse order: "
echo $x
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
8 comments… add one
  • anonymous Nov 4, 2016 @ 11:57

    can anyone explain ..how this script works..plz

  • Annonymous Jun 14, 2012 @ 8:21

    Is there a way to get the result to print out on separate lines? e.g.

    Numbers are: 1 2 3 4
    Reverse Order:
    1
    2
    3
    4

  • dileep Feb 16, 2012 @ 19:56

    it is so usefull for students……

  • Micah Trueblood Jan 25, 2012 @ 6:06

    Thanks a lot for the article.Thanks Again. Fantastic.

  • Aneesh Jan 3, 2012 @ 6:49

    I am getting wrong results.. Is there anything wrong???

    [root@www ~]# sh reverse.sh 3,4,9,12
    Numbers are : 3,4,9,12
    Reverse order: 3,4,9,12
    [root@www ~]# cat reverse.sh
    #!/bin/bash
    # Write a shell program that outputs all integers upto the command line
    # parameter starting from 1 and also should output the same numbers
    # in the reverse order.
    # --------------------------------------------------------------------
    # This is a free shell script under GNU GPL version 2.0 or above
    # Copyright (C) 2005 nixCraft project.
    # Feedback/comment/suggestions : http://cyberciti.biz/fb/
    # -------------------------------------------------------------------------
    # This script is part of nixCraft shell script collection (NSSC)
    # Visit http://bash.cyberciti.biz/ for more information.
    # -------------------------------------------------------------------------
    if [ $# -eq 0 ]
    then
            echo "$0 num1, num2, numN"
            exit 1
    fi
    x=""
    echo -n "Numbers are : "
    for n in $@
    do
      echo -n $n
      echo -n " "
      x="$n $x"
    done
    echo ""
    echo -n "Reverse order: "
    echo $x
    [root@www ~]#
    
    • Arun Jan 10, 2012 @ 14:48

      Hey, CLA`s cannot be passed to the script using commas,
      sh reverse.sh 3 4 9 12

      Use space instead of comma. :)

      Cheers,
      Arun D Patil

  • Quetzal Apr 27, 2011 @ 19:17

    if [ $# -lt 1 ]; then echo “Usage: $(basename $0) arg1 arg2 … argN “; exit 1; fi

    echo “Numbers are: $@”
    for n in $@; do x=”$n $x”; done
    echo “Reverse order: $x”

    • Aryan Mar 2, 2012 @ 17:39

      Thanks This is easiest and shortest way

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.