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".
More on Variables, quoting and expansions
What are the rules for assigning variables in Bash?BeginnerWhat is the difference between single quotes, double quotes and `$'...'`?BeginnerWhy should you almost always quote variable expansions? Show what goes wrong.BeginnerExplain the difference between `"$@"`, `"$*"`, `$@` and `$*`.IntermediateWhat do `${var:-default}`, `${var:=default}`, `${var:+alt}` and `${var:?msg}` do?IntermediateHow do you strip prefixes and suffixes with parameter expansion?Intermediate