After setting:
var1="a\nb"
var2="$var1"
I expect $var1
and $var2
to be equal; however:
[[ $var1 == $var2 ]] && echo yes || echo no
returns no
. Why is that?
After setting:
var1="a\nb"
var2="$var1"
I expect $var1
and $var2
to be equal; however:
[[ $var1 == $var2 ]] && echo yes || echo no
returns no
. Why is that?
You have to quote to perform the string comparison:
[[ "$var1" == "$var2" ]]
So this works:
$ var1="a\nb"
$ var2="$var1"
$ [[ "$var1" == "$var2" ]] && echo yes || echo no
yes
From the comments, chepner indicates:
Without the quotes, $var2 is interpreted as a pattern, not a string, and the \n is treated as a regular n.