Checking if a Number Is Even or Odd


One of the simplest applications we can use the modulo operator for is to check if a number is even or odd.
We can do this by using the modulo operator with 2 as the divisor. If the result is 0, then the number is even. Otherwise, it’s odd.
Let’s look at an example to demonstrate this:

$ n=10
$ if [ $((n % 2)) -eq 0 ]; then
>   echo "Number $n is even"
> else
>   echo "Number $n is odd"
> fi
Number 10 is even
$ n=7
$ if [ $((n % 2)) -eq 0 ]; then
>   echo "Number $n is even"
> else
>   echo "Number $n is odd"
> fi
Number 7 is odd