Issue
Please tell why printing odd numbers in bash script with the following code gives the error:
line 3: {1 % 2 : syntax error: operand expected (error token is "{1 % 2 ")
for i in {1 to 99}
do
rem=$(( $i % 2 ))
if [$rem -neq 0];
then
echo $i
fi
done
Solution
This is working example:
for i in {1..99}
do
rem=$(($i % 2))
if [ "$rem" -ne "0" ]; then
echo $i
fi
done
- used
for
loop have a typo in minimum and maximum number, should be{1..99}
instead of{1 to 99}
- brackets of the
if
statement needs to be separated withwhitespace
character on theleft
and on theright
side - Comparision is done with
ne
instead ofneq
, see this reference.
As already pointed out, you can use this shell checker if you need some clarification of the error you get.
Answered By - DevDio Answer Checked By - David Marino (WPSolving Volunteer)