TARGET contains the value alpha beta.
Pass the whole value as one argument to printf.
Which expansion is appropriate?
Double quotes expand the variable while suppressing word splitting and pathname expansion, preserving one argument.
Detailed explanation
printf '<%s>\n' $TARGETIncorrect. Unquoted expansion undergoes word splitting and can produce two arguments.
Incorrect. Unquoted expansion undergoes word splitting and can produce two arguments.
printf '<%s>\n' '$TARGET'Incorrect. Single quotes prevent variable expansion and pass the literal text $TARGET.
Incorrect. Single quotes prevent variable expansion and pass the literal text $TARGET.
printf '<%s>\n' "$TARGET"Correct. "$TARGET" expands to one argument containing the space.
Correct. "$TARGET" expands to one argument containing the space.
printf '<%s>\n' \$TARGETIncorrect. Escaping the dollar sign prevents expansion.
Incorrect. Escaping the dollar sign prevents expansion.
Try it yourself
An example you can run in a temporary verification environment.
TARGET='alpha beta'; printf '<%s>\n' "$TARGET"Expected result
<alpha beta>Key points
- Double quotes still expand
- The result stays one argument
- Unquoted expansion splits words
Notes
- Environment: Bash 5.x / shell builtin printf
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.