Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.
In Bash, quoting controls which characters the shell treats as syntax and which it passes as text. Use single quotes to preserve every character literally, double quotes to prevent word splitting and glob expansion while still allowing variable and command substitutions, and backslashes to protect individual characters. Bash also supports ANSI-C quoting with $'…'.
What quoting changes in Bash
Bash processes command text before starting a command. Characters such as spaces, dollar signs, asterisks, and quotes can affect parsing, expansion, or how arguments are formed. Quoting protects characters from some or all of those operations. It does not generally add quote characters to the argument: the quotes are shell syntax and are removed during processing.
The examples here describe GNU Bash 5.3, as documented in the GNU Bash Reference Manual, last updated May 18, 2025. Bash-specific forms such as $'…' should not be assumed to work in every shell or in POSIX sh.
Free tools Windows power users keep installed
One-click scans. No signup required.
Single quotes: preserve text literally
Within single quotes, every enclosed character keeps its literal value. Bash does not expand variables or command substitutions, interpret backslashes as escapes, or treat spaces and wildcard characters as shell syntax.
#1 Best Overall
- Used Book in Good Condition
message='Price: $5; path is *.txt'
In this assignment, message receives the literal text Price: $5; path is *.txt. The dollar sign and asterisk do not trigger expansion.
Including a single quote
A single-quoted span cannot contain a single quote—not even by putting a backslash before it. Instead, end the span, add an escaped quote outside it, and start another span:
text='It's Bash'
Here the backslash protects the apostrophe outside the single-quoted spans. Bash joins the adjacent quoted and unquoted pieces into one word, producing It's Bash.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Double quotes: protect words, allow selected expansions
Double quotes keep spaces and wildcard characters from splitting an argument or expanding as filename patterns, but they do not make everything literal. Bash still performs parameter expansion, command substitution, and arithmetic expansion inside double quotes. Backticks also retain their command-substitution meaning.
name='Ada Lovelace'
printf '%sn' "$name"
The quoted expansion is passed as one argument, including the space. By contrast, "$name" still expands the variable; use single quotes or escape the dollar sign when you need the literal characters $name.
Inside double quotes, a backslash only protects $, backtick, double quote, backslash, or newline. Before other characters, the backslash remains part of the text.
Rank #4
Backslashes: escape a character or continue a line
An unquoted backslash protects the character immediately after it, removing that character’s special meaning. For instance, file name.txt forms one word containing a space. An unquoted backslash followed by a newline is removed, allowing a command to continue onto the next physical line.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →printf '%sn' one
two
The line continuation joins the lines; it does not add a newline argument between one and two. A backslash-newline inside single quotes is literal text. Within double quotes, backslash-newline is also removed.
Best Value
ANSI-C quoting: Bash’s $'…' form
Bash’s dollar-single-quote form interprets backslash escapes in the enclosed text. It is useful when a string needs characters such as a newline or tab, or an escaped quote, without writing a literal line break.
printf '%sn' $'first linensecond line'
The escape n becomes a newline in the resulting string. This is Bash-specific syntax; check the target shell before using it in a script intended for other shells.
Why a quoted argument may still behave unexpectedly
Quoting protects text during specific shell-processing stages; it does not make a command’s own interpretation disappear. A command may interpret an argument as an option, pattern, expression, or filename after Bash has passed it. Likewise, double-quoting a variable prevents word splitting and pathname expansion on that expansion, but does not stop the variable from expanding.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteQuick Recap
- Use
"$var"when the variable’s value should be one argument, even if it contains spaces or wildcard characters. - Use
'$var'when you want the literal characters$var, not the variable’s value. - Use
$outside quotes or"$"inside double quotes to protect a dollar sign in a short literal. - Use single quotes for literal text, but end the single-quoted span to include an apostrophe.
Quick comparison
| Form | Variable and command substitutions | Spaces and wildcard characters | Typical use |
|---|---|---|---|
'text' |
Not performed | Kept literal | Text that must not expand |
"text" |
Performed | Do not split or expand as filename patterns | One argument containing expanded values |
x outside quotes |
Protects the next character | Depends on which character is protected | Escaping an individual special character |
$'text' |
Backslash escapes are interpreted | Quoted as one word | Bash-specific escaped characters such as newlines |
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

