Linux Quoting, Spaces and Case Sensitivity
Spaces separate words unless protected
Before a simple command runs, Bash interprets its command line. Unquoted spaces between literal words separate arguments. Quote text that must stay together. Quote characters mark boundaries for the shell; the receiving command normally does not receive those quote characters. For file examples, open a terminal in the supplied lab-fixtures/command-basics/sample-data folder (or use a file manager’s Open in Terminal action). It contains notes.txt, Notes.txt, daily report.txt, .hidden and -draft. The examples only read these files. The exact display order depends on locale; the examples below assume the C locale and GNU ls.
printf '[%s]\n' daily reportprintf '[%s]\n' 'daily report'Here printf repeats its format for the extra text arguments. The brackets show where each argument begins and ends. The quoted version prints one line because the two words are one argument.
The first command receives two text values, daily and report, so each appears between its own pair of brackets. The second receives a single text value containing a space. The space inside the second pair of brackets is data, not an argument separator.
Single quotes, double quotes and backslash
For fixed text containing spaces, single quotes or double quotes can keep the text together. Single quotes preserve the literal contents; double quotes still allow some shell expansions such as $HOME, which will be covered later. A backslash can protect an individual space. Use a fixed printf format rather than inserting arbitrary text into the format itself.
cat 'daily report.txt'cat "daily report.txt"cat daily\ report.txtEach spelling passes the same filename. Without quotes or the escaped space, cat daily report.txt would attempt two separate names; in this fixture neither is present.
Case matters
On a usual case-sensitive Linux filesystem, these names can refer to separate files. A case-insensitive mount can behave differently; the supplied fixture requires case-sensitive storage. Command names also normally require their exact spelling: ls is not LS.
Practice
Use the bracketed printf format to print two words as one argument and end as a second argument. Predict the two output lines. Remember that quoting a filename beginning with - does not stop a program from treating it as an option.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Quoting spaces and case sensitivity — Knowledge Check
Apply the command-line concept in a safe beginner lab.
How many text arguments follow the format in printf "[%s]\n" "a b"?
Which passes daily report.txt as one filename?
On the fixture filesystem, are Notes.txt and notes.txt identical names?
Which quote style preserves literal $HOME?
Does quoting "-draft" stop GNU ls option parsing?