my@comp:~/wtfdir$ cat wtf.sh
str1=$(echo "")
str2=$(echo "")
if [ $str1 != $str2 ]; then
echo "WTF?!?!"
fi
my@comp:~/wtfdir$ ./wtf.sh
WTF?!?!
my@comp:~/wtfdir$
WTF is going on here?!
How I wrote the above code: Googling "bash compare strings" brought me to this website which says:
You can check the equality and inequality of two strings in bash by using if statement. “==” is used to check equality and “!=” is used to check inequality of the strings.
Yet I'm getting the above?
What am I not understanding? What am I doing wrong?
[ "$str1" != "$str2" ][isn't special syntax, it's just a command; its arguments go through the same expansion phases as every other command. Ifls $emptyruns justlswith no arguments, then[ $empty != $empty ]just runs[ != ]. (The special-syntax alternative totestis[[, and it doesn't have the problem you're asking about here).str1=is sufficient to assign an empty value tostr1.