Default values and unset versus null
How it works
Parameter expansion lets you choose a value without starting another program. A variable is unset if it has no assigned value; name= instead assigns an empty (null) string. ${name-word} supplies word only when name is unset. ${name:-word} also supplies it when name is empty. Neither form changes the variable. Use braces to separate a name from adjoining text, and quote the result to preserve whitespace. The fallback can itself be another expansion and is evaluated only when needed.
Run each example separately in a noninteractive Bash 5.2 script in a fresh empty practice directory, with default options and C locale. Output is shown separately; do not enter it as code.
Unset value
Expected stdout:
[untitled][untitled]Both forms use the fallback.
Deliberately empty
Expected stdout:
[][untitled]Only the colon form treats empty as missing.
Fallback does not assign
label=printf '[%s]' "${label:-daily report}" "$label"label=readyprintf '[%s]' "${label:-untitled}"Expected stdout:
[daily report][][ready]The first expansion supplies a value, but label remains empty until the explicit assignment.
Common mistakes
Empty does not mean unset. Choose whether a deliberate empty setting should be respected; use the non-colon form if it should. A fallback is not an assignment.
Practice
For an unset, empty and nonempty variable named label, print a fallback of untitled using both operators. Keep the empty value only in the non-colon case.
Next: Assign defaults and report missing values