Alternate-value expansions
How it works
Use ${p+word} to produce a marker if p is set, even if its value is empty. ${p:+word} produces the marker only for a nonempty value. If the condition is not satisfied the expansion is empty; it never substitutes the original value or assigns a new value. This is useful for showing whether an optional setting was supplied without printing its contents. Quoting an empty result in an argument preserves an empty argument, rather than removing the argument.
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
Expected stdout:
[][]Neither condition is met.
Empty
Expected stdout:
[supplied][]The variable exists, but its value is empty.
Nonempty
note='draft text'printf '[%s]' "${note+supplied}" "${note:+nonempty}" "$note"Expected stdout:
[supplied][nonempty][draft text]Both markers appear; the original variable is unchanged.
Common mistakes
:+ is not a fallback for a missing variable: it has the opposite trigger. A marker is not a check that a value is valid, only that it is nonempty.
Practice
Print supplied if note exists, even if it is empty; print nonempty only when it contains text. Test all three states.
Next: Length and substring expansion