Single Quotes vs. Double Quotes
Bash gives you two everyday ways to wrap text in quotation marks, and they behave very differently. Single quotes ('...') keep every character exactly as you typed it. Double quotes ("...") keep most characters literally but still let Bash substitute variables, command output and arithmetic results. Knowing which quote to reach for decides whether a command receives the text you intended or something Bash rewrote on the way.
Why Quoting Exists
Before Bash runs a command, it reads the line and processes it in stages. It splits the line into words at spaces, then replaces variables, runs command substitutions, expands wildcards such as *, and finally hands the resulting words to the program. Several characters have a special meaning during that process: the space, $, *, ?, ;, &, |, <, >, (, ), # and the quote characters themselves.
Quoting is how you tell Bash, "treat this text as plain data, not as syntax." The two quote styles differ in how much of that processing they switch off.
| Feature | Unquoted | Double quotes | Single quotes |
|---|---|---|---|
Variable expansion ($name) | Yes | Yes | No |
Command substitution ($(cmd)) | Yes | Yes | No |
Arithmetic expansion ($((1+2))) | Yes | Yes | No |
| Splitting into words at spaces | Yes | No | No |
Wildcard expansion (*.txt) | Yes | No | No |
Tilde expansion (~) | Yes | No | No |
| Backslash escapes | Yes | Only before a few characters | No |
Single Quotes: Everything Is Literal
Inside single quotes, Bash does no interpretation at all. Every character stands for itself, including $, backticks and backslashes. The only character that has meaning is the closing single quote.
name="Ana"
echo 'Hello, $name'
echo "Hello, $name"
Output:
Hello, $name
Hello, Ana
The first command prints the characters $name because the single quotes stopped Bash from looking at the dollar sign. The second command lets Bash replace $name with its value, Ana.
Single quotes are the right choice whenever you want text to arrive untouched: regular expressions, awk programs, sed scripts, and strings that contain many $ or `` characters.
echo 'Price: $5, path: C:\Users, glob: *.txt'
Output:
Price: $5, path: C:\Users, glob: *.txt
None of those special characters did anything, because nothing inside single quotes is interpreted.
Double Quotes: Literal Except for a Few Characters
Double quotes protect spaces and wildcards, but Bash still performs the substitutions that begin with $ or a backtick. Three kinds of expansion keep working inside double quotes:
echo "Home: $HOME"
echo "Sum: $((2 + 3))"
echo "Today is $(echo Friday)"
Output (the home directory depends on your account):
Home: /root
Sum: 5
Today is Friday
$HOMEis a variable expansion. Bash replaces it with the value of the variable.$((2 + 3))is arithmetic expansion. Bash calculates the result and inserts it.$(echo Friday)is command substitution. Bash runs the command and inserts its output.
Later lessons cover the last two in depth. For now, the important point is that double quotes leave them switched on.
Escaping Inside Double Quotes
A backslash inside double quotes only has special meaning in front of five characters: $, a backtick, ", ``, and a newline. In front of anything else, the backslash stays as an ordinary character.
echo "Price: \$5 and a backslash \\ and a quote \" and backtick \` "
echo "Not special: \n \t \a"
Output:
Price: $5 and a backslash \ and a quote " and backtick `
Not special: \n \t \a
The first line uses the backslash to print a literal dollar sign, backslash, double quote and backtick. In the second line, \n, \t and \a are not on the list, so Bash passes both characters through unchanged. This also means that echo "a\tb" does not print a tab. Lesson 47 shows the tools that do.
The Apostrophe Problem
Because a single quote ends a single-quoted string, you cannot put an apostrophe inside single quotes, not even with a backslash. There are two common ways around it.
The simplest is to use double quotes for text that contains an apostrophe:
echo "It's fine"
Output:
It's fine
When the text must stay inside single quotes, close the quote, add an escaped apostrophe, and reopen the quote:
echo 'It'\''s fine'
Output:
It's fine
Read 'It'\''s fine' as three pieces joined together with no spaces between them: 'It', then \' (an escaped, literal apostrophe), then 's fine'. Bash glues adjacent pieces into one word.
Quotes and Word Splitting
After expanding a variable, Bash splits the unquoted result into separate words wherever it finds spaces, tabs or newlines. This is called word splitting. Double quotes switch it off, so the whole value stays in one piece.
file="my notes.txt"
printf '[%s]\n' $file
printf '[%s]\n' "$file"
Output:
[my]
[notes.txt]
[my notes.txt]
printf '[%s]\n' prints each argument it receives inside square brackets, which makes the argument boundaries visible. The unquoted $file was split into two arguments, my and notes.txt. The quoted "$file" stayed as one argument. If a command like rm $file ran with the unquoted form, it would try to remove two files named my and notes.txt instead of one file named my notes.txt.
Quoting also preserves runs of spaces in literal text:
echo Hello World
echo "Hello World"
Output:
Hello World
Hello World
Without quotes, Bash sees two words and echo prints them separated by a single space. With quotes, Bash sees one word that contains the extra spaces, and echo prints it as is.
An empty variable shows the same effect:
empty=""
printf '[%s]\n' $empty "$empty"
Output:
[]
The unquoted $empty vanished and produced no argument at all. The quoted "$empty" produced one empty argument. Only one line was printed because printf received only one argument.
Quotes and Wildcards
Wildcard characters are expanded only when they are unquoted. Both kinds of quotes switch globbing off.
mkdir -p g && cd g && touch a.txt b.txt && cd ..
echo g/*.txt
echo "g/*.txt"
echo 'g/*.txt'
Output:
g/a.txt g/b.txt
g/*.txt
g/*.txt
The unquoted pattern was replaced by the names of the matching files. The two quoted versions were printed literally. The same rule applies to ~: unquoted ~ becomes your home directory, while "~" and '~' stay as a tilde character.
Mixing Quote Styles in One Word
Quoted and unquoted pieces that touch each other form a single word, so you can switch styles in the middle of an argument. This lets you keep most of a string literal while still expanding one variable:
name="Ana"
echo 'single '"double $name"' single $name'
Output:
single double Ana single $name
The word has three parts:
'single 'is literal text."double $name"is expanded, so$namebecomesAna.' single $name'is literal again, so$namestays as typed.
A common practical version is building a command string where most of it must stay literal but one value must be inserted:
dir="/var/log"
echo 'Searching in '"$dir"' for $HOME-style names'
Output:
Searching in /var/log for $HOME-style names
Protecting a Variable Name from Following Text
When a variable is followed by letters, digits or underscores, Bash reads them as part of the variable name. Braces mark exactly where the name ends:
fruit=apple
echo "${fruit}s and $fruits."
Output:
apples and .
${fruit}s expanded the variable named fruit and then added the letter s. $fruits was read as a different variable named fruits, which does not exist, so it expanded to nothing.
Strings That Span Several Lines
Both quote styles allow a string to continue across lines. The newline becomes part of the string:
echo "line1
line2"
echo 'a
b'
Output:
line1
line2
a
b
If you end a line with a backslash inside double quotes, Bash removes both the backslash and the newline, joining the lines:
echo "abc\
def"
Output:
abcdef
Inside single quotes the backslash would stay in the text, because nothing is interpreted there.
ANSI-C Quoting with $'...'
Bash has a third quote form, $'...', which understands escape sequences such as \t for a tab and \n for a newline. It is covered in the next lesson, but a first look shows how it differs from the two quote styles above:
printf '%s\n' $'tab:\there' $'newline:\nnext' $'it\'s'
Output:
tab: here
newline:
next
it's
Inside $'...', a backslash is an escape character, and \' is allowed to produce a literal apostrophe.
Passing Code to Another Shell
A frequent use of single quotes is giving a command string to another program that will interpret it later. With bash -c, single quotes keep the inner shell's variables from being expanded by the outer shell:
bash -c 'echo "inner: $0"' demo
x=outer
bash -c "echo outer-expanded: $x"
Output:
inner: demo
outer-expanded: outer
In the first command, the single quotes delivered the text echo "inner: $0" intact, and the child shell expanded $0 itself. The word demo after the command string becomes $0 inside the child shell. In the second command, the double quotes let the outer shell expand $x first, so the child shell received echo outer-expanded: outer and had no variable left to expand.
The same distinction matters for ssh host '...', sudo sh -c '...', find -exec sh -c '...' and similar commands.
Quoting Arrays
An array expanded with @ inside double quotes produces one word per element, even when elements contain spaces. Without quotes, each element is split further:
arr=("a b" "c d")
printf '[%s]\n' ${arr[@]}
printf '[%s]\n' "${arr[@]}"
Output:
[a]
[b]
[c]
[d]
[a b]
[c d]
The unquoted form turned two elements into four words. The quoted form kept the two original elements. Arrays get their own lessons later in the series.
Choosing Between the Two
The decision usually follows one question: should Bash look inside this text?
- Use single quotes when the text must stay exactly as written. Patterns for
grep,sedandawk, strings full of$and ``, and code handed to another shell all fit here. - Use double quotes when the text contains variables, command substitutions or arithmetic that Bash should expand, or when a value may contain spaces and must stay one word.
- Use no quotes only when you want word splitting and wildcard expansion, such as
ls *.txtorfor n in 1 2 3. - Combine the styles in one word when a mostly literal string needs one expanded value.
grep '^Error: ' app.log
grep "^Error: $code" app.log
The first command passes the pattern literally. The second inserts the value of $code into the pattern before grep sees it.
In an interactive terminal, double quotes have one more catch: the exclamation mark can trigger history expansion, so a line such as echo "Hello!" may behave unexpectedly. Single quotes avoid this, and scripts are not affected because history expansion is turned off for them.