Difference between single and double quotes in Bash
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Bash, single quotes and double quotes both stop unwanted word splitting, but they do not mean the same thing. Single quotes preserve text almost completely literally, while double quotes still allow some shell expansions to happen.
That difference affects variable substitution, command substitution, escaping, and how safely your script handles spaces or special characters. Most quoting bugs in shell scripts come from knowing that quoting matters but not knowing which kind of quoting is needed.
What Single Quotes Do
Single quotes preserve the literal value of everything between them. Bash does not expand variables, command substitutions, or backslash escapes inside single quotes.
Output:
This makes single quotes the safest choice when you want exact text.
Single quotes are especially useful for:
- regular expressions passed as literal strings
- text that contains dollar signs
- commands where expansion would be dangerous or misleading
What Double Quotes Do
Double quotes still protect against word splitting and pathname expansion, but they allow a few important expansions, especially variable expansion and command substitution.
Here, $name and $(date ...) are expanded before the command runs. That is why double quotes are the normal choice when you want to interpolate variables safely.
Double quotes are often the right default in Bash because they preserve whitespace while still allowing expansion.
Why Quoting Prevents Bugs
Consider a filename with spaces:
With double quotes, Bash passes one argument to rm. Without quotes, Bash would split the value into separate words. Single quotes would also keep it as one argument, but they would not let $file expand at all.
This is why experienced shell users often say: if you want expansion, use double quotes; if you want literal text, use single quotes.
Escaping Rules Are Different
Backslashes behave differently inside the two quote styles. Inside single quotes, a backslash is just a backslash. Inside double quotes, backslash can still escape certain special characters.
The first command prints a dollar sign because the backslash prevents variable expansion. The second prints the backslash too, because single quotes treat both characters literally.
A literal single quote inside a single-quoted string is awkward and usually written by closing and reopening the quoted string:
That syntax looks strange, but it is a standard shell pattern.
Special Cases Worth Remembering
Double quotes still allow these expansions:
- '
$variable' - '
$(command)' - arithmetic expansion like
$((1 + 2))
They do not allow wildcard glob expansion on the quoted text itself, so "*.txt" stays as the literal string *.txt.
One especially important shell detail is "$@", which preserves the original positional arguments as separate items. That is a good example of double quotes doing something useful that single quotes cannot do.
Choosing The Right Quote Style
A practical rule set is:
- use single quotes for fixed literal text
- use double quotes for variable expansion that must remain one argument
- avoid unquoted expansions unless you intentionally want splitting and globbing
That rule is not just style. It directly affects correctness and security when scripts process user input, filenames, or shell fragments.
Common Pitfalls
- Using single quotes around a variable and then wondering why it does not expand.
- Leaving variables unquoted and breaking on spaces in filenames.
- Assuming double quotes disable all shell processing.
- Forgetting that backslashes behave differently inside single and double quotes.
- Writing shell commands that accidentally rely on word splitting instead of explicit quoting.
Summary
- Single quotes preserve text literally and prevent shell expansion.
- Double quotes preserve argument boundaries but still allow variable and command substitution.
- Use double quotes for most variable expansions in scripts.
- Use single quotes when you need exact literal text.
- Good quoting prevents bugs with spaces, wildcards, and special characters.

