user2851669 user2851669 - 3 months ago 22
Bash Question

Different behavior on escaping double quotes

I have a empty variable. When I do

if [[ -z "$value" ]]; then echo "hello"; fi

the output is hello

However, when I escape double quotes, i.e.

if [[ -z \"$value\" ]]; then echo "hello"; fi

hello is not printed on the screen.

What is the difference between above two commands?


It is because the in the first statement, the variable is expanded and the resulting statement is null.

Whereas in the second it expands to "". To see the difference consider the two echo statements,

$ echo "$value"

$ echo \"$value\"

The first one is null where as the second is not.