Putting the if
statement in the assignment is rather clumsy and easy to get wrong. The more standard way to do this is to put the assignment inside the if
:
if [ 2 = 2 ]; then
mathstester="equal"
else
mathstester="not equal"
fi
As for testing variables, you can use something like if [ "$b" = 2 ]
(which'll do a string comparison, so for example if b is "02" it will NOT be equal to "2") or if [ "$b" -eq 2 ]
, which does numeric comparison (integers only). If you're actually using bash (not just a generic POSIX shell), you can also use if [[ "$b" -eq 2 ]]
(similar to [ ]
, but with somewhat cleaner syntax for more complicated expressions), and (( b == 2 ))
(these do numeric expressions only, and have very different syntax). See BashFAQ #31: What is the difference between test, [ and [[ ? for more details.