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

in Categories Decision Making last updated April 5, 2008
```#!/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 part of nixCraft shell script collection (NSSC)
# -------------------------------------------------------------------------

echo -n "Enter numnber : "

rem=\$(( \$n % 2 ))

if [ \$rem -eq 0 ]
then
echo "\$n is even number"
else
echo "\$n is odd number"
fi```

## 15 comment

1. Jolly says:

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

1. Bobby says:

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

2. alex says:

how to check the entered number is grater than 10

3. kutbuddeen ansari says:

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

4. keyur says:

echo “enter the number”

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

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

5. Sammy Le Jerk says:

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

6. Saikiran says:

echo “Program to check even or odd number”
echo “Enter a number”
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

7. nibash says:

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

1. Sammy Le Jerk says:

nevermind

8. Myd says:

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

9. Jon disnard says:

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

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

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

12. Aakash Alfred says:

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

1. novendra says:

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