$_ CLI In Depth
Learning progress 0%
lesson expansion quoting word-splitting

Single, Double & $'...' Quotes

5 min read Module 8: Expansion, Quoting & Word Splitting Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:14

Single quotes are literal

Everything between a pair of single quotes is taken literally. Dollar signs, backslashes, backticks, asterisks, and spaces all lose their special meaning. A single quote cannot appear inside single quotes, even when escaped with a backslash, because the backslash itself is literal there. That single limit is the reason the ANSI-C form and the quoting trick in the escaping lesson exist. Use single quotes for any text that must reach a program unchanged, such as a regular expression, an awk program, or a string that contains dollar signs.

BASH2 lines
echo '$HOME is literal'
echo '*.txt stays as typed'
TEXT2 lines
$HOME is literal
*.txt stays as typed

Double quotes keep most expansions

Double quotes stop word splitting and filename expansion, but they still allow parameter expansion, command substitution, and arithmetic expansion. Inside double quotes, a backslash only has special meaning before a dollar sign, a backtick, a double quote, another backslash, or a newline. Any other backslash is kept. A single quote inside double quotes is an ordinary character. Double quotes are therefore the default for any argument that holds a value, which is why the advice to quote variables appears in almost every shell guide.

BASH4 lines
name='Ada Lovelace'
echo "Hello, $name"
echo "A single ' is fine here"
echo "a\tb stays as a backslash and t"
TEXT3 lines
Hello, Ada Lovelace
A single ' is fine here
a\tb stays as a backslash and t

Interactive bash has one more hazard inside double quotes. History expansion can rewrite text that contains an exclamation mark before the command runs, so a password-like string with an exclamation mark may need single quotes when you type it at the prompt. Scripts do not perform history expansion, so the problem appears mainly at the keyboard.

The ANSI-C form

The form $'...' is a bash extension, sometimes called ANSI-C quoting. Inside it, backslash escapes are processed in the same way as in C. The common ones are \t for tab, \n for newline, \\ for a backslash, and \' for a single quote. Hexadecimal escapes such as \x41 and Unicode escapes such as \u00e9 are also accepted. This form is the easiest way to put a tab or newline into an argument, and it is also the answer to the question of how to include a single quote in a string that you want to write with single quotes.

BASH2 lines
echo $'col1\tcol2'
echo $'it\'s one line\nand a second'
TEXT3 lines
col1	col2
it's one line
and a second

The first line contains a real tab character between the two columns. The second line shows that the escaped quote and the newline are both handled by the ANSI-C rules.

Adjacent pieces form one word

Quoting styles can be mixed in one word without spaces. The pieces are joined, and quote removal takes away each set of quote characters. The result is one argument built from literal text, expanded text, and escaped characters. This is convenient for building a single argument from several parts, and it is also the reason some quoting bugs are hard to see. Read the word from left to right, and mark each quote pair as you go.

BASH2 lines
v=mid
echo 'start-'"$v"$'\tend'
TEXT1 lines
start-mid	end

The locale form and when to use it

The form $"..." asks bash to translate the string through the message catalog for the current locale. Scripts that ship translations use it, and without a catalog it behaves exactly like double quotes. You will rarely write it yourself, but it is useful to recognize it when you read other people's scripts.

Mixing quotes in a real command

Most real commands mix styles, and reading them is a skill of its own. Take a line that searches a log for a value held in a variable, where the log name contains a space. The search pattern needs double quotes so the variable is filled in. The file name needs quotes so the space stays inside one argument. Fixed options such as -n need no quotes at all. Reading the line piece by piece, each quoted region protects exactly what it encloses, and the unquoted pieces are plain words that bash will split at spaces.

BASH2 lines
code=404
grep -n "status $code" 'access log.txt'

Writing the line this way keeps the file name and the pattern separate, so a change to one does not affect the other. It is the same discipline that the splitting and globbing lessons described, applied to every argument in the command.

Quoting inside awk and other programs

Single quotes are the normal choice for programs that have their own dollar signs, such as an awk script. In awk '{ print $1 }', the single quotes stop bash from expanding $1, so awk receives the field reference it expects. If the same program were written in double quotes, bash would try to expand $1 as a positional parameter, which is almost never what you intend. When the program needs a value from a shell variable, close the single quotes around the program and insert the variable with double quotes, a pattern you will see in many real scripts.

Choosing a quote style

A simple rule covers most cases. Use single quotes for fixed text that contains special characters, double quotes for any value you need expanded, and the ANSI-C form only when you need an escape such as a tab or newline. When you are unsure, quote with double quotes and read the result with printf '[%s]\n', which shows exactly where the argument begins and ends. The lab in this lesson asks you to produce three values that each use one of these forms.

Word splitting and quoting a value with spaces

Visualizer

An unquoted variable is split into words at spaces, tabs and newlines. A filename with a space breaks unless the expansion is quoted.

1 / 5
value of f
m
y
␠
f
i
l
e
.
t
x
t
IFS
space, tab, newline (default)

One variable holds one string with a space in it. The shell only sees characters; it does not know the value is a filename.

f="my file.txt"; touch "$f"; ls $f; echo "status $?"; ls "$f"
Terminal lab · 3 steps · real Linux in your browser

Choose the right quote form

Write one literal, one escape-based, and one escaped-quote string to files in /workspace.

01
Keep a dollar sign literal
Single quotes stop every expansion, including the one for the dollar sign. Run this in /workspace. ``bash echo '$HOME is literal' > single.txt `` The file should contain the text with the dollar sign and the word HOME.
Show hint for step 1
Put the whole text in single quotes and redirect to single.txt.
02
Write a real tab with ANSI-C quotes
The escape \t inside $'...' becomes a real tab character. Regular double quotes would keep the two letters backslash and t. ``bash echo $'col1\tcol2' > tab.txt `` The file should contain col1, a tab, and col2.
Show hint for step 2
Use the $'...' form with \t between the two words, and redirect to tab.txt.
03
Include double quotes and an apostrophe
Inside double quotes, an apostrophe is an ordinary character, and a backslash in front of a double quote keeps it literal. This prints the text it's "quoted". ``bash echo "it's \"quoted\"" > mixed.txt `` The file should contain it's followed by "quoted" in double quotes.
Show hint for step 3
Use double quotes, and escape each inner double quote with a backslash. Redirect to mixed.txt.
Check yourself Question 1 of 4
Concept Check Literal dollar sign recall

Which command prints the literal text $HOME without expanding it?

Recap & Key Takeaways

Single quotes keep every character literal, but they cannot contain a single quote.
Double quotes allow parameter, command, and arithmetic expansion, and they stop word splitting and globbing.
The $'...' form interprets backslash escapes such as \t, \n, and \'.
Adjacent quoted pieces without spaces form one word, which bash joins before running the command.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue