Answer the question
In order to leave comments, you need to log in
How to solve if..else problem in bash?
Hello!
I'm writing a game:
#! /bin/bash
while true
do
a=$((1 + RANDOM % 10))
b=$((1 + RANDOM % 10))
res=$a+$b
echo -n "$a + $b = "
read path
echo ""
if
then
echo "It's true"
else
echo "It's false!!!"
fi
done
It's false!!!
Answer the question
In order to leave comments, you need to log in
By default, bash creates all variables as text, so
res=$a+$b gives you string concatenation, not number addition.
Use explicit arithmetic addition through res=$(( $a + $b ))
or declare all variables in advance as integer through
declare -ia
declare -ib
declare -i res
declare -i path
And ideally, comparison instead of make it numeric
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question