Preserve spaces and empty arguments
How it works
An argument can contain spaces or even be empty. In an ordinary command, double-quote scalar variable expansions to preserve a single argument. Without quotes, Bash can split the value on IFS whitespace and expand filename patterns; these steps are detailed next chapter. To inspect arguments, use bash -c with a fixed script string and pass data as separate arguments. The first value after the script becomes $0, and the rest become positional parameters. printf %q displays a Bash-escaped representation of an argument rather than its raw text.
Run each example separately in a fresh noninteractive Bash script with default options. Tested on Bash 5.2.37 with C locale. Do not enter the prompt or the expected output as code.
Count the arguments
label='Daily report'bash -c 'printf "count=%s\n" "$#"' demo "$label"bash -c 'printf "count=%s\n" "$#"' demo $labelExpected stdout (tabs are real characters):
count=1count=2With default IFS and no wildcard in the value, the unquoted expansion splits at the space; the quoted expansion does not.
Empty versus missing
label=bash -c 'printf "count=%s\n" "$#"' demo "$label"bash -c 'printf "count=%s\n" "$#"' demo $labelExpected stdout (tabs are real characters):
count=1count=0An explicit quoted empty expansion remains an argument; an unquoted empty scalar expansion in this position disappears.
Inspect escaped representations
Expected stdout (tabs are real characters):
daily\ report''\*In Bash 5.2.37 with C locale, this output represents a space, an empty argument and a literal star. The quoting style is implementation-dependent.
Common mistakes
printf '[%s]\n' without any data arguments can still print [] because missing string conversions default to empty text. Do not infer the argument count from that output alone. Use $# to count. Do not quote an entire multiword command as a substitute for quoting its arguments.
Practice
Pass North office, an empty argument, and * to a fixed child Bash script; print the argument count and each argument with %q.
Next: Safe printf format strings