9

I need to check if a number is even.

Here's what I've tried.

newY="281"
eCheck=$(( $newY % 2 ))

echo $newY
echo $eCheck
while [ $eCheck -eq 0 ]; do
        newY=$((newY-1))
        eCheck=$(( $newY % 2 ))
        echo $newY
done

... returns eCheck = 1 how can it be? 281/2 = 140.5

i've also tried using bc, but it went into an infinite loop eCheck=$(echo "scale=1;$newY%2" | bc)

codeforester
  • 39,467
  • 16
  • 112
  • 140
rabotalius
  • 1,430
  • 5
  • 18
  • 27
  • 2
    +1 for well defined problem and an attempt to solve it!. Turn on shell debugging, you'll quickly see where there is a problem. Use `set -vx` near the top of your file. Good luck. – shellter Jul 31 '13 at 02:56

9 Answers9

10

Nici is right, "%" is the modulo, and gives you the remainder of the division.

Your script can be simplified as follows :

if [[ $((var % 2)) -eq 0 ]];
   then echo "$var is even"; 
   else echo "$var is odd"; 
fi
Ahmad
  • 8,811
  • 11
  • 76
  • 141
jderefinko
  • 647
  • 4
  • 6
6

You can do a simple :

eCheck=$(( $newY & 1 ))

to utilize the bitwise operators in bash.

blackSmith
  • 3,054
  • 1
  • 20
  • 37
2

The % operator computes the remainder. So 281 % 2 is 1, because 281 divided by 2 is 140 with a remainder of 1.

rici
  • 234,347
  • 28
  • 237
  • 341
2
#!/usr/bin/env bash

[[ $( expr $1 % 2 ) -eq 0 ]] && echo "Even Number" || echo "Odd Number"
double-beep
  • 5,031
  • 17
  • 33
  • 41
Sharma_Ji
  • 31
  • 3
  • Expression, $ ( expo $1 % 2) The $ sign prior the expression, assigns the expression as a variable and feed for comparison. $1 : used to access the argument passed on as bash filename.sh – Sharma_Ji Feb 10 '19 at 18:10
  • 1.( expr $1 % 2 ) does mathematical evaluation for arg passed ( $1 ) on the script. 2.( $( expr $1 % 2 ) -eq 0) here -eq stands for equals to and checks if remainder is zero or not. Making a $ sign prior the mathematical evaluation feed the remainder as a feedback the next executing command. 3. && is the binary AND its executed iff the condition statement prior it is true. 4. || is the binary OR, it executes iff the condition statement prior to it is false. – Sharma_Ji Feb 12 '19 at 04:52
1

You are so close! Think of it like this. There are only two possible answers for Y in the expression

Y = X % 2

for ALL values of X. What are they? Play with a few values of X to see if you can come up with the values for Y.

Next, is there anything you can determine about what the value of Y says about the value of X? That is, can you use the value of Y to answer the problem you are trying to solve?

Jeff N
  • 3,249
  • 1
  • 12
  • 4
0
#!bin/bash
echo "Type the input integer, followed by [Enter]:"
read x
if [ $((x%2)) -eq 0 ]; then 
  echo "$x is even"
else
  echo "$x is odd"
fi

Yes "%" is modulo, it gives you the remainder like others have mentioned
  • There is a syntax error in this answer. The correct way to check is: `if ((x%2 == 0)); then ...`. – codeforester Dec 17 '19 at 06:03
  • While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained. – borchvm Feb 25 '20 at 07:55
0

This can be done using expr

evenCheck=$(expr $newY % 2)
if [ $evenCheck = 0 ] ;
    then echo "number is even"; 
else echo "number is odd";
fi 
done
Saurabh
  • 7,525
  • 4
  • 45
  • 46
0

As you mention you are checking even for single no, So there is no need to use a loop. Here is my bit of code.

read -p "Enter a number: " num
if [ $((num%2)) -eq 0 ]
then
  echo "Entered Number is even:"
else
  echo "Entered Number is odd:"
fi
jeb
  • 78,592
  • 17
  • 171
  • 225
  • 2
    This seems to be a duplicate, or where is your answer different to the ones of [@jderefinko 6 years old](https://stackoverflow.com/a/17964581/463115) or the one of Harpreet Khanduja? – jeb Dec 17 '19 at 06:00
  • In Harpreet one, you might get the wrong output you just need to replace -eq 0]; to -eq 0 ]. "space between 0 and bracket". – OM Prakash Singh Feb 21 '20 at 10:03
0

Since this question is tagged as Bash, the right way to check if a number is even in Bash is:

if ((num%2 == 0)); then
    echo "The number is even"
fi

or, more even shorter:

if ((num % 2)); then
    echo "The number is even"
fi

We don't need to use [[ ... ]] in this case.


See also:

codeforester
  • 39,467
  • 16
  • 112
  • 140