1
echo "enter one no"
read n

rem='expr $n % 2'


if [ $rem -eq 0 ]

then
    echo "Number $n is even"
else
    echo "Number $n is odd"
fi

while executing this program , bash is showing message like "too many arguement ".

2 Answers 2

2

That's because you are doing:

if [ expr $n % 2 -eq 0 ]

How come?

Because

rem='expr $n % 2'

is assigning variable rem as string expr $n % 2.

You need, command substitution:

rem=$(expr $n % 2)

Also use bash's native arithmetic operator instead of expr:

rem=$(( $n % 2 ))
0
  • use read's built in prompt feature read -p "Enter one num" n

  • prefer using $( . . .) , single quotes don't expand commands. rem="$(expr $n % 2)"

  • Quote the variables when using them "$rem"

  • use at most one line of break between if and then

    if [ "$rem" -eq 0 ] then

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .