Shell script to find whether an input number is palindrome or not

A palindrome is a word, phrase, number or other sequence of units that has the property of reading the same in either direction (the adjustment of punctuation and spaces between words is generally permitted). Composing literature in palindromes is an example of constrained writing. The word “palindrome” was coined from Greek roots palin (“back”) and dromos (“way, direction”) by English writer Ben Jonson in the 1600s.

A palindromic number is a number where the digits, with decimal representation usually assumed, are the same read backwards, for example, 58285. They are studied in recreational mathematics where palindromic numbers with special properties are sought. A palindromic prime is a palindromic number that is a prime number.

# Shell script to find whether an input number is palindrome or not
# -----------------------------------------------
# Copyright (c) 2006 nixCraft <>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit for more information.
# -------------------------------------------------------------------------
echo -n "Enter number : "
read n
# store single digit
# store number in reverse order
# store original number
while [ $n -gt 0 ]
    sd=$(( $n % 10 )) # get Remainder 
    n=$(( $n / 10 ))  # get next digit
    # store previous number and current digit in reverse 
    rev=$( echo ${rev}${sd} ) 
if [ $on -eq $rev ];
  echo "Number is palindrome"
  echo "Number is NOT palindrome"
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics via:
Category List of Unix and Linux commands
Disk space analyzers df ncdu pydf
File Management cat tree
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities NetHogs 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 Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
11 comments… add one
  • krishan chandra pandey Sep 4, 2017 @ 6:44

    #check the no is palendrome or not
    echo “enter the no to check palendrome”
    read n
    while [ $n -ne 0 ]
    x=$(( n % 10 ))
    y=$(( y * 10 + x ))
    n=$(( n / 10 ))
    echo “y is $y”
    echo “number is $m”
    if [ $m -eq $y ]
    echo “value is palendrome”
    echo “value is not palendrome”

  • anshul Aug 4, 2016 @ 5:52

    echo “Enter a String or number : ”
    read string or number
    if [ “$(echo $string | rev)” = “$string” ]
    echo “\”$string\” IS a Palindrome”
    echo “\”$strin\” IS NOT a Palindrome”

  • Nitin Jul 22, 2013 @ 18:07

    please give a program without using rev function….
    use only while loop to write a program

  • santhu Aug 16, 2012 @ 5:37

    can you provide the code for n number of inputs and n number of outputs in shell scripting for verilog code and that code is generated in another file as like .v

    i ll waiting for your reply and send me as soon as possible

  • kritanta kr roy Jul 8, 2012 @ 23:26

    jyothi you are ………. i have no word to express it ,,,u are great

  • Krutika Modi Oct 9, 2011 @ 3:25

    echo “enter any no:”
    read no
    no=`expr $no`
    no1=`expr $no1`
    rev=`expr 0`
    while [ $no -gt 0 ]
    temp=`expr $no % 10`
    rev=`expr $temp + $rev \* 10`
    no=`expr $no / 10`
    if [ $no1 -eq $rev ];
    echo “$no1 is a palindrom no”
    echo “$no1 is not a palindrom no”

    • arun May 2, 2012 @ 11:14

      nhi chal raha

  • jyothi Nov 9, 2010 @ 9:45

    Shell script to find whether an input number is palindrome or not

    Simple script for the above is
    echo “Enter the string:”
    read s
    rvs=`echo $s | rev`
    if [ $s = $rvs ]
    echo “It is palindrome”
    echo “It is not”

    • Anonymous Sep 18, 2011 @ 7:27

      jyothi ji ki jai ho!!!

    • Sonu Yadav Sep 28, 2011 @ 5:41

      What is rev in this program

  • buggi Dec 13, 2009 @ 7:25

    could you please help in providing me the sample code in printing all the prime numbers
    in the given range ?

Leave a Reply

Your email address will not be published.

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