Shell script to read a number and find whether the number is odd or even

#!/bin/bash
# Shell script to read a number and find whether the number is odd or even
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter numnber : "
read n
 
rem=$(( $n % 2 ))
 
if [ $rem -eq 0 ]
then
  echo "$n is even number"
else
  echo "$n is odd number"
fi
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
15 comments… add one
  • Jolly Oct 28, 2017 @ 16:57

    I don’t understand the part
    – eq0
    In finding odd and even number

    • Bobby Nov 23, 2017 @ 2:44

      -eq is a symbolof equal and 0 represent rem is zero

  • alex Sep 29, 2017 @ 7:13

    how to check the entered number is grater than 10

  • kutbuddeen ansari Feb 9, 2017 @ 18:14

    I want to find even and odd number (1 to n) using loop

  • keyur Sep 19, 2016 @ 7:08

    echo “enter the number”
    read n

    2num=$(( $n % 2 ))
    if [ $num -eq 0 ]

    then
    echo “$n number is even”
    else
    echo “$n number is odd”
    fi

  • Sammy Le Jerk May 16, 2014 @ 5:41

    read n
    n2=`expr $n % 2`
    if [ $n2 -eq 0 ];
    then
    echo “even”
    else
    echo “odd”
    fi

  • Saikiran Dec 3, 2012 @ 17:29

    echo “Program to check even or odd number”
    echo “Enter a number”
    read n
    a=`expr $n % 2`
    if [ $a -eq 0 ] ; then #Semicolon is most important for Executing if-else
    echo “It is an even number”
    else
    echo “It is an odd number”
    fi

  • nibash Oct 6, 2012 @ 9:59

    if[ $($n % 2) -eq 0]
    then
    echo “even”
    else
    echo “odd”

    • Sammy Le Jerk May 16, 2014 @ 5:36

      nevermind

  • Myd Sep 3, 2012 @ 13:56

    odd() { let ${1: -1}%2 && return 0; return 1; }

  • Jon disnard Mar 23, 2012 @ 2:53

    is_even() {
    case $(( 10#$1 % 2 )) in
    0) return 0 ;;
    *) return 1 ;;
    esac
    }

  • bhaskar Feb 1, 2011 @ 5:23

    correct it as:
    if [ $rem -eq ‘0’ ]

  • Kristada673 Aug 22, 2010 @ 8:38

    @Aakash Alfred: No buddy, that doesn’t work. It displays everything as odd……..

  • Aakash Alfred Jun 24, 2010 @ 5:48

    # On the third line: Instead of rem=$(( $n % 2 ))
    # You can use ;
    rem= `expr $n % 2`

    • novendra Aug 10, 2011 @ 12:29

      $(( $n % 2 ))
      this syntax not work properly in shell script
      error in brackets

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.