Shell variables and quoting

Lesson 24 of 30 · Working with Linux

A shell variable gives a value a name in the current shell. Assignment has no spaces around =:

label='field notes'
printf '<%s>\n' "$label"
printf '<%s>\n' '$label'

The first output is <field notes>; the second is <$label>. Double quotes allow $label to expand while preserving the value as one argument. Single quotes keep the dollar sign literal. Quoting expansions matters: an unquoted expansion can be split into words and interpreted as filename patterns. printf '%s\n' "$label" is a reliable way to display the value, including spaces.

The braces in "${label}" delimit the name, which is useful when text follows it: "${label}_old" expands label, then adds _old. Names are case-sensitive: label and LABEL are different. A value assigned here belongs to this shell session; another already-running shell does not automatically receive the change.

unset label

unset removes the variable from the current shell. It does not erase copies already inherited by other processes. A missing variable and an empty value can have different meanings to a program. Variables are convenient for configuration, but their contents are not automatically private; do not print secrets or place them in shared logs or shell history.