Reputation: 13
I think i'm missing something very obvious. But shouldn't the following code produce the opposite response? I thought if the statement "if s == d" is used and s is not equal to d then the if statement should return false and not run the following code. This is not what appears to happen. Can anyone explain what i've missed. I think it's something very obvious.
Thanks
s=2
d=3
if ! [ "$s == $d" ]; then echo "hello"; fi
if [ "$s == $d" ]; then echo "hello"; fi
hello
Upvotes: 0
Views: 678
Reputation: 123450
You are quoting the entire string "$s == $d"
when you should be quoting the two arguments "$s"
and "$d"
.
This means that instead of comparing $s
to $d
, you are checking whether "2 == 3"
is a non-empty string (which it is).
This will correctly print "not equal":
s=2
d=3
if ! [ "$s" == "$d" ]; then echo "not equal"; fi
if [ "$s" == "$d" ]; then echo "equal"; fi
Upvotes: 2