Modulo calculation in shell script

hi
i am writing a progrm to print the even numbers and the code which i am following is as follows

#!/usr/bin/bash
echo "enter a number a"
read a
if [$a % 2 == 0]
then
  echo "the number is even"
else
  echo "the number is odd"
fi

~
what is the mistake i am doing ...please tell me

Can you try as below.
Note: Please use code tags [c ode] ... . . [/code] when posting codes,script,data etc

if [ $a%2 -eq 0 ]

When you compare values in bash use the (-eq) thing with the integers, and the (==) if you have strings...

Could be written (with arithmetic evaluation and array)
#!/usr/bin/bash
parity=(even odd) # parity[0]='even' parity[1]='odd'
read -p "enter a number: " a
echo "the number is ${parity[$((a%2))]}

1 Like