Quotes and escaping
On this page
You want to pass the name meeting notes.txt to a command. Without protection, Bash passes two words instead. The file’s name has not changed; the command receives different arguments from the ones you intended.
By the end, you can predict argument boundaries, choose quotes or an escape, and recover when an unfinished quote leaves Bash waiting.
Before you start
Section titled “Before you start”Finish Bash and other shells (B08.1). Keep your B04 lab directory. This lesson uses only ~/lab/b08/quotes.
mkdir -p ~/lab/b08/quotescd ~/lab/b08/quotesNo files need to exist for the argument examples. printf will print text rather than open a filename. Use Reset this lesson before repeating the setup.
Make each argument visible
Section titled “Make each argument visible”The format [%s] asks printf to put each supplied string inside brackets. Its \n adds a newline. With extra arguments, printf reuses this format, so you see one argument per line.
Predict first: how many bracketed lines will the following command print?
printf '[%s]\n' meeting notes.txtRun it, then compare with your guess
printf '[%s]\n' meeting notes.txt[meeting][notes.txt]Two lines. The unquoted space separates words; it is not part of either argument. The bracket format is another argument, which tells printf how to display these strings.
Protect the space with single quotes:
printf '[%s]\n' 'meeting notes.txt'[meeting notes.txt]The quotes group the text as one word. Bash removes these syntactic quotes before the command receives the argument; the quotation marks themselves do not appear inside the brackets. B04.1’s setup and B06.3’s spaced filenames relied on this.
Choose how to protect text
Section titled “Choose how to protect text”Double quotes also keep spaces together:
printf '[%s]\n' "meeting notes.txt"[meeting notes.txt]A backslash escape can protect one following character, such as a space:
printf '[%s]\n' meeting\ notes.txt[meeting notes.txt]This is why Tab completion in B06.3 could insert a backslash before a filename’s space. The escape is input syntax; the command receives the space, without that backslash.
learner@rootvolt:~/lab/b08/quotes$ printf '[%s]\n' meeting notes.txt[meeting]
[notes.txt]learner@rootvolt:~/lab/b08/quotes$ printf '[%s]\n' 'meeting notes.txt'[meeting notes.txt]learner@rootvolt:~/lab/b08/quotes$ printf '[%s]\n' "meeting notes.txt"[meeting notes.txt]learner@rootvolt:~/lab/b08/quotes$ printf '[%s]\n' meeting\ notes.txt[meeting notes.txt]learner@rootvolt:~/lab/b08/quotes$ Steps (12)
- Type
printf '[%s]\n' meeting notes.txt - Press Enter
- Output:
[meeting] [notes.txt] - Type
printf '[%s]\n' 'meeting notes.txt' - Press Enter
- Output:
[meeting notes.txt] - Type
printf '[%s]\n' "meeting notes.txt" - Press Enter
- Output:
[meeting notes.txt] - Type
printf '[%s]\n' meeting\ notes.txt - Press Enter
- Output:
[meeting notes.txt]
The static transcript contains the same boundaries. Several unquoted spaces separate words too; they do not create extra empty arguments between those words.
| What you type after the format | Arguments received |
|---|---|
meeting notes.txt |
meeting, then notes.txt |
'meeting notes.txt' |
One: meeting notes.txt |
"meeting notes.txt" |
One: meeting notes.txt |
meeting\ notes.txt |
One: meeting notes.txt |
The formats in this table are fixed examples, not an interactive shell in the browser.
Keep a dollar sign literal
Section titled “Keep a dollar sign literal”HOME names your home directory. In shell input, $HOME asks Bash to replace that reference with its value. That replacement is expansion; B08.3 explains variables and assignments.
Predict first: will single quotes and double quotes preserve the same text here?
printf '[%s]\n' '$HOME'printf '[%s]\n' "$HOME"Run it, then compare with your guess
printf '[%s]\n' '$HOME'[$HOME]printf '[%s]\n' "$HOME"[/home/learner]Single quotes preserve the dollar sign and letters literally. Double quotes allow this variable expansion while keeping the resulting value together. The captured home belongs to learner; your account’s home path can differ.
Outside quotes, a backslash can protect the dollar sign itself:
printf '[%s]\n' \$HOME[$HOME]Use single quotes for text you want Bash to preserve literally. Use double quotes when you want expansion and one resulting argument. Inside double quotes, a backslash has special behavior only before certain characters; it is not a general instruction to remove every backslash.
Separate shell quoting from printf formatting
Section titled “Separate shell quoting from printf formatting”Look at this argument inside brackets:
printf '[%s]\n' '\n'[\n]The second argument contains a backslash and the letter n. Single quotes leave both unchanged. %s prints that argument as text; it does not interpret its backslash as a newline.
Compare it with the format itself:
printf 'alpha\nbeta\n'alphabetaHere printf interprets \n in its format. Bash still passed it unchanged. The same distinction explains the \r and \t formats used in B07.5: printf, rather than the single quotes, creates those control characters.
learner@rootvolt:~/lab/b08/quotes$ printf '[%s]\n' '\n'[\n]learner@rootvolt:~/lab/b08/quotes$ printf 'alpha\nbeta\n'alpha
betalearner@rootvolt:~/lab/b08/quotes$ Steps (6)
- Type
printf '[%s]\n' '\n' - Press Enter
- Output:
[\n] - Type
printf 'alpha\nbeta\n' - Press Enter
- Output:
alpha beta
Keep the format fixed and pass text through %s. A value containing formatting characters should remain data, rather than becoming the format itself.
Cancel an unfinished quote
Section titled “Cancel an unfinished quote”Type the following incomplete line and press Enter. Its final single quote is deliberately missing:
printf '%s\n' 'unfinishedlearner@rootvolt:~/lab$ mkdir -p ~/lab/b08/quoteslearner@rootvolt:~/lab$ cd ~/lab/b08/quoteslearner@rootvolt:~/lab/b08/quotes$ printf '%s\n' 'unfinished> ^Clearner@rootvolt:~/lab/b08/quotes$ printf '%s\n' 'finished'finishedlearner@rootvolt:~/lab/b08/quotes$ Steps (11)
- Type
mkdir -p ~/lab/b08/quotes - Press Enter
- Type
cd ~/lab/b08/quotes - Press Enter
- Type
printf '%s\n' 'unfinished - Press Enter
- Output:
> - Press Ctrl + C
- Type
printf '%s\n' 'finished' - Press Enter
- Output:
finished
The captured > is Bash’s continuation prompt: it needs more input before it can finish reading the command. B03.4 showed this wait; now you know what it means. No printf command has executed yet. Press Ctrl+C, then enter the complete version:
printf '%s\n' 'finished'finishedDo not type a second shell command at the continuation prompt to diagnose it: those characters can become part of the unfinished quoted text.
Mistakes to recognize
Section titled “Mistakes to recognize”Expecting quotes to reach the program. Syntactic quotes protect or group text and are then removed. The brackets show what the command actually receives.
Using typographic quotation marks. Use straight ' and ", as in the code blocks. Curved quotation marks copied from prose do not have these shell-quoting roles.
Reading the continuation prompt as an error message. > is waiting for more syntax. Ctrl+C cancels the incomplete line and returns to the ordinary prompt.
Try it yourself
Section titled “Try it yourself”Goal: Pass two arguments after the bracket format: the phrase review notes and the literal text $HOME. The phrase must stay together, and the dollar sign must remain visible.
Hint
Single quotes both preserve literal text and protect spaces. Keep the format as the first argument.
Solution
printf '[%s]\n' 'review notes' '$HOME'[review notes][$HOME]Two bracketed lines establish both boundaries. The second contains the dollar sign, not a home path.
Reset this lesson
Section titled “Reset this lesson”Return to the parent and inspect only this lesson’s directory:
cd ~/lab/b08ls -AR quotesrm -r ~/lab/b08/quotesRerun the setup under Before you start. If the directory is already absent, skip removal. Close any program using this directory first.
- Unprotected spaces separate words; quotes or a backslash can preserve a space inside one argument.
- Single quotes preserve literal text; double quotes allow variable expansion while grouping the result.
- Bash passes single-quoted backslashes unchanged;
printfinterprets escapes in its format. - An unfinished quote produces a continuation prompt; Ctrl+C cancels it.
Next, Variables and environment basics explains where expanded values come from and which ones a child process inherits.
Further reading
Section titled “Further reading”- On your system:
help printfandman bash, sections QUOTING and EXPANSION. - GNU Bash: Quoting and Word Splitting.
- GNU Bash: Bash Builtin Commands, the
printfentry.