Single and double quotes both let a shell treat spaces and special characters as part of one argument. The difference is what Bash and Zsh are allowed to expand inside them.
Use straight ASCII quote characters in shell commands:
- Single quote:
' - Double quote:
"
Curly typographic quotes such as ‘, ’, “ and ” are ordinary characters to the shell. Commands copied from word processors and formatted websites can therefore behave differently or fail entirely.
Single quotes preserve literal text
Everything between a pair of single quotes is treated literally. Parameter expansion, command substitution and arithmetic expansion do not run:
printf '%s\n' 'This is my $PATH'The output is:
This is my $PATHUse single quotes when the text must reach the command exactly as written. They are particularly useful for regular expressions, SQL fragments and strings containing $, *, ?, backticks or spaces.
Double quotes allow expansion
Double quotes still keep the result together as one argument, but parameter expansion and command substitution take place:
printf '%s\n' "This is my $PATH"The output contains the current value of PATH:
This is my /usr/local/sbin:/usr/local/bin:/usr/binThe exact directories vary between systems. Command substitution also works inside double quotes:
printf '%s\n' "Today is $(date +%A)"Quoting the complete string prevents spaces in the expanded value from creating extra arguments. As a general rule, quote parameter expansions unless you specifically need the shell to split the result or treat it as a filename pattern:
cp -- "$source_file" "$destination_directory"Include a single quote in single-quoted text
A backslash has no special meaning inside ordinary single quotes. This does not escape the apostrophe:
# Invalid shell syntax
printf '%s\n' 'It's cold outside'End the single-quoted section, add an escaped quote, and begin another single-quoted section:
printf '%s\n' 'It'\''s cold outside, no kind of atmosphere'The shell joins the adjacent quoted and escaped pieces into one argument:
It's cold outside, no kind of atmosphereWhen the string does not contain values that could expand, double quotes are easier to read:
printf '%s\n' "It's cold outside, no kind of atmosphere"Bash and Zsh also support ANSI-C quoting, where a backslash can escape the apostrophe:
printf '%s\n' $'It\'s cold outside, no kind of atmosphere'The $'...' form is useful for escape sequences such as \n and \t, but it should not be used in scripts that must run in a strictly POSIX sh environment.
Bash and Zsh differences
The single- and double-quote rules above work in both shells. There are a few option-dependent differences around the edges:
- Interactive history expansion can give
!a special meaning inside double quotes. The active options and interactive mode determine the exact behaviour. - Zsh's optional
RC_QUOTESsetting allows two adjacent single quotes inside a single-quoted string to represent one quote. The close, escape and reopen form shown above works without relying on that option.
For portable Bash and Zsh commands, use the common quoting forms rather than depending on shell options. The Bash reference manual and Zsh quoting documentation describe the complete rules.