Quoting - single, double and escapes
How Bash decides where one word ends and the next begins, and the three tools that override it: single quotes, double quotes and the backslash.
Quoting - single, double and escapes
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Bash splits a command line into words at unquoted whitespace before it runs anything, so a space inside a value changes how many arguments a command receives.
- Single quotes make every character literal. Nothing inside them is expanded, and a single quote cannot appear inside them.
- Double quotes keep spaces and globs literal but still expand `$variables`, `$(commands)` and backslash escapes for a handful of characters.
- A backslash quotes the one character after it. `$'...'` gives you C-style escapes such as `\t` and `\n`.
- Quote every variable expansion unless you specifically want it split and globbed.
Why quoting exists
Type a command and Bash has to answer a question before it can run anything: where does each word start and stop? Its rule is simple. Whitespace separates words. That rule is exactly what you want most of the time, and exactly wrong when a value contains a space.
I made a file whose name has a space, put the name in a variable, and used the variable the obvious way:
$ file="report 2026.txt"
$ ls -l $file
ls: 2026.txt: No such file or directory
ls: report: No such file or directory
$ ls -l "$file"
-rw-r--r--@ 1 reader wheel 0 Nov 23 02:39 report 2026.txt
(The owner column is edited here, since it showed my real username.) ls was never told about a file called report 2026.txt. It was handed two separate arguments. Quoting is how you tell Bash “treat this stretch of text as part of one word, and don’t interpret it”. There are three tools, and they differ in how much they switch off.
Single quotes: nothing happens
Everything between single quotes is literal. No variables, no globs, no escapes.
$ name=world
$ echo 'hello $name'
hello $name
$ echo "hello $name"
hello world
Think of single quotes as a sealed envelope: Bash passes the contents through untouched. That makes them the right choice for patterns you want another program to interpret, like a grep or awk expression. The one limitation is that a single quote can’t appear inside them, because there is no escape. The workaround is to close the quote, add a backslash-escaped one, and reopen:
$ echo 'it'\''s fine'
it's fine
$ echo "It's fine"
It's fine
When the text contains an apostrophe, double quotes are usually the easier route.
Double quotes: spaces are safe, expansions still run
Double quotes protect whitespace and wildcards but still allow $ expansion, backticks, and backslash. That is why "$file" works: the variable is replaced, and the result is not split afterwards.
$ printf '[%s]\n' $file
[report]
[2026.txt]
$ printf '[%s]\n' "$file"
[report 2026.txt]
printf prints one bracketed line per argument, which makes it a good argument counter. Try it whenever you are unsure how a command line was split.
Spaces collapse without quotes and survive with them, because the unquoted version becomes separate arguments that echo rejoins with single spaces:
$ echo "a b"; echo a b
a b
a b
A glob is only live outside quotes:
$ echo *.txt "*.txt" '*.txt'
report 2026.txt *.txt *.txt
Inside double quotes, a backslash only escapes five characters: $, backtick, ", \ and newline. Any other backslash stays. I confirmed that printf '%s\n' "double quotes keep \n too, but \\ collapses" printed \n unchanged and \\ as a single \.
The backslash and $'...'
A backslash outside quotes quotes exactly one character: echo \* prints a literal star, and \ keeps a space inside a word. Handy for one-offs, tedious in bulk.
For control characters, Bash has ANSI-C quoting. $'...' interprets \t, \n and friends:
$ echo $'tab:\there\nnext line'
tab: here
next line
It works in macOS’s /bin/bash 3.2 as well as in Bash 5.3. I ran both. It is Bash (and zsh) syntax, not POSIX sh.
Quote the variable, almost always
Quotes don’t nest the way you may expect: " and ' are just ordinary characters inside each other. The habit that matters is this: put double quotes around every $variable and $(command) unless you can say why you want splitting. The leaving-out case is rare, and the failure when you get it wrong, such as rm $file on a name with a space, deletes the wrong thing. The word splitting lesson shows exactly when the splitting happens.
Interactive-only wrinkle: in an interactive Bash, ! can trigger history expansion inside double quotes. echo "!x" printed !x in my non-interactive run, because history expansion is off in scripts. Single quotes always avoid it.
About the video
“bash quoting is really not that difficult!” from anthonywritescode is eight minutes, published in 2022. I could not read a transcript. The description says it covers “the small number of rules to bash quoting” and the '"'"' idiom, and the audio and description are in English. Anthony Sottile is a well-known Python and open-source maintainer. The '"'"' idiom is a variation on the close-and-reopen trick shown above, with a double-quoted apostrophe in the middle instead of a backslash. A second video from You Suck at Programming is linked for a deeper look at escaping and heredocs.
Where this fits
Quoting decides what is one word. Next comes what lives in the words: variables and the environment.
Check yourself
A variable holds `report 2026.txt`. Why does `ls -l $file` fail, and how do you fix it?
The unquoted expansion is split on the space, so ls receives two arguments, `report` and `2026.txt`. Write `ls -l "$file"` so it arrives as one.
Which quotes would you use to print the text `$HOME` literally, and why?
Single quotes, as in `echo '$HOME'`. Nothing inside single quotes is expanded, while double quotes would replace `$HOME` with its value.
How do you put a single quote inside a single-quoted string?
You cannot, so you close the quote, add an escaped quote, and reopen: `'it'\''s'`. Using double quotes, `"it's"`, is usually simpler.
Inside double quotes, which characters still do something special?
Dollar sign, backtick and backslash, plus the history `!` in an interactive shell. Everything else, including spaces and `*`, is literal.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.