Unset and empty variables
What you need to know
An unset variable has no assigned value; an empty variable is set to a string of length zero. With default Bash options, expanding either as a standalone double-quoted scalar argument produces one empty argument. Use unset name to remove an assignment; name= assigns an empty value. The expansion ${name-word} uses word only when unset, while ${name:-word} also uses it when empty. These are previews of parameter expansion, covered in depth later.
Run each example separately as a new Bash script (Chapter 1). Tested with Bash 5.2.37 on Debian 13, default shell options; no personal configuration is required.
Compare the printed values
unset labelprintf 'unset=[%s]\n' "$label"label=printf 'empty=[%s]\n' "$label"Expected stdout:
unset=[]empty=[]Brackets make the empty result visible. The output is identical even though the variable states differ.
Default only when unset
unset labelprintf '%s\n' "${label-fallback}"label=printf '[%s]\n' "${label-fallback}"Expected stdout:
fallback[]The expansion does not assign fallback to the variable. An explicitly empty value is preserved.
Default when unset or empty
label=printf '%s\n' "${label:-Guest}"label='Neda'printf '%s\n' "${label:-Guest}"Expected stdout:
GuestNedaUse the colon form when empty input also means a default is appropriate.
Common mistakes
An empty value can be intentional, for example an optional suffix. Do not replace it automatically unless the contract says so. With nounset enabled, a direct expansion of an unset variable may fail; shell options are taught later.
Practice
Set nickname empty, compare ${nickname-Guest} with ${nickname:-Guest}, then unset it and repeat.
Next lesson: Readonly variables and declare