Quoting and expansions
Quick answer
Quoting keeps a filename or report label together as one argument. Bash expands variables, command substitutions and pathname patterns before invoking a command.
Prepare the practice environment
Run python3 lab-fixtures/shell-scripting/setup.py as an ordinary user from the extracted package root in a disposable Linux environment. Enter the printed directory, check pwd, then run export LC_ALL=C; umask 077; set -o pipefail. Use a fresh fixture for each example. Save the code as example.sh inside that directory and run bash --noprofile --norc -o pipefail example.sh rather than sourcing it. Stop if a command fails unexpectedly. All output paths must be new. The source of check-report.sh is in the fixture JSON. Outputs below are not commands.
How it works
Single quotes protect literal text; double quotes allow parameter and command substitution while protecting their results from ordinary word splitting and globbing. $(command) captures stdout and removes trailing newline characters. An unquoted variable expansion may split into words and then match filenames; quote it unless that behavior is deliberate. A glob such as *.txt is expanded by the shell, not by the receiving command. With default Bash settings an unmatched glob remains literal. Use a fixed printf format, not a variable as the format.
For name containing daily report.txt, the quoted expansion passes one argument; the unquoted expansion passes two in the usual whitespace-splitting environment. Quoting does not become part of the argument itself. Command substitution removes all trailing newline characters, but preserves internal newlines in the captured value. Quote the result when passing it as one argument; if exact trailing bytes matter, keep the data in a file or stream instead.
Try the examples
name='daily report.txt'printf '[%s]\n' $nameprintf '[%s]\n' "$name"The unquoted form is an intentional mistake shown only with controlled input.
Expected stdout:
[daily][report.txt][daily report.txt]Redirection supplies the file as input; wc outputs the count without a filename.
Expected stdout:
lines=2The quoted pattern remains literal; the unquoted glob produces one argument per matching name.
Expected stdout:
[daily report.txt][notes.txt][*.txt]Common mistakes
Never rebuild a command from arbitrary input using eval. Quote variables and pass each operand separately; for tools accepting options, consider -- where supported.
Practice
Use a variable containing daily report.txt to read the file safely, then explain the difference between quoting a glob and quoting its result.
Related references
Study: printf, bash. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Quoting and expansions — Knowledge Check
Apply the lesson concepts to the controlled shell fixture.
What does "$name" preserve?
What does $(command) capture?
Who expands *.txt in Bash?
Which format is safer for arbitrary text?