user2851669 user2851669 - 1 year ago 60
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download