Interview Prep/Shell Scripting/Variables, quoting and expansions

What is the difference between `unset var`, `var=` and `var=""`? How do you test which state a variable is in?

IntermediateConceptVariables, quoting and expansions
Answer

unset var removes it. var= and var="" set it to the empty string; it exists but is empty.

[[ -v var ]]          # true if set (even empty), Bash 4.2+
[[ -n "${var-}" ]]    # true if set and non-empty, safe under set -u
[[ -z "${var+x}" ]]   # true if unset (portable idiom)

This matters for config: "unset" usually means "use the default", "empty" may mean "explicitly disabled".

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Variables, quoting and expansions