Skip to content

Quotes and escaping

Module
How the Shell Interprets Input
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
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.

Finish Bash and other shells (B08.1). Keep your B04 lab directory. This lesson uses only ~/lab/b08/quotes.

Terminal window
mkdir -p ~/lab/b08/quotes
cd ~/lab/b08/quotes

No 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.

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?

Terminal window
printf '[%s]\n' meeting notes.txt
Run it, then compare with your guess
Terminal window
printf '[%s]\n' meeting notes.txt
Captured output
[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:

Terminal window
printf '[%s]\n' 'meeting notes.txt'
Captured output
[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.

Double quotes also keep spaces together:

Terminal window
printf '[%s]\n' "meeting notes.txt"
Captured output
[meeting notes.txt]

A backslash escape can protect one following character, such as a space:

Terminal window
printf '[%s]\n' meeting\ notes.txt
Captured output
[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.

Spaces separate words unless protected
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Unprotected spaces separate arguments; quotes and an escaped space preserve one argument.
Steps (12)
  1. Type printf '[%s]\n' meeting notes.txt
  2. Press Enter
  3. Output: [meeting] [notes.txt]
  4. Type printf '[%s]\n' 'meeting notes.txt'
  5. Press Enter
  6. Output: [meeting notes.txt]
  7. Type printf '[%s]\n' "meeting notes.txt"
  8. Press Enter
  9. Output: [meeting notes.txt]
  10. Type printf '[%s]\n' meeting\ notes.txt
  11. Press Enter
  12. 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.

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?

Terminal window
printf '[%s]\n' '$HOME'
printf '[%s]\n' "$HOME"
Run it, then compare with your guess
Terminal window
printf '[%s]\n' '$HOME'
Captured output
[$HOME]
Terminal window
printf '[%s]\n' "$HOME"
Captured output
[/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:

Terminal window
printf '[%s]\n' \$HOME
Captured output
[$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:

Terminal window
printf '[%s]\n' '\n'
Captured output
[\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:

Terminal window
printf 'alpha\nbeta\n'
Captured output
alpha
beta

Here 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.

The shell preserves a backslash; printf can interpret it
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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. Single quotes preserve backslashes; printf interprets the newline escape in its format, while %s prints its argument literally.
Steps (6)
  1. Type printf '[%s]\n' '\n'
  2. Press Enter
  3. Output: [\n]
  4. Type printf 'alpha\nbeta\n'
  5. Press Enter
  6. 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.

Type the following incomplete line and press Enter. Its final single quote is deliberately missing:

Terminal window
printf '%s\n' 'unfinished
Cancel an unclosed quote
learner@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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. The missing closing quote produces the continuation prompt; Ctrl+C cancels it before a complete command prints finished.
Steps (11)
  1. Type mkdir -p ~/lab/b08/quotes
  2. Press Enter
  3. Type cd ~/lab/b08/quotes
  4. Press Enter
  5. Type printf '%s\n' 'unfinished
  6. Press Enter
  7. Output: >
  8. Press Ctrl + C
  9. Type printf '%s\n' 'finished'
  10. Press Enter
  11. 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:

Terminal window
printf '%s\n' 'finished'
Captured output
finished

Do not type a second shell command at the continuation prompt to diagnose it: those characters can become part of the unfinished quoted text.

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.

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
Terminal window
printf '[%s]\n' 'review notes' '$HOME'
Captured output
[review notes]
[$HOME]

Two bracketed lines establish both boundaries. The second contains the dollar sign, not a home path.

Return to the parent and inspect only this lesson’s directory:

Terminal window
cd ~/lab/b08
ls -AR quotes
Terminal window
rm -r ~/lab/b08/quotes

Rerun 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; printf interprets 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.