10

I used to use '' and "" (single and double-quotes) interchangeably on the command line, but I recently noticed that '$HOME/some/dir' is not expanded, while "$HOME/some/dir" is. I searched around a little bit and found that "" allows some protection of special characters, while '' allows full protection. So what other characters are protected by '' and not ""?

EDIT: What are practical situations in which these differences might be significant?

ilkkachu
  • 138,973
apoorv020
  • 1,283

2 Answers2

14

Take a look at the bash man page. There's an entire section on quoting. Because this licensed under the GFDL, which is not compatible with the CC-BY-SA license used here, I won't quote the whole thing, but really reading that is the most definitive answer.

In summary, single quotes stop all interpretation -- the string is rendered literally. Double quotes leave $ (dollar sign), ` (backquote) as special, and \ (backslash) as special when followed by certain other characters. And ! will be treated specially if the history expansion feature is enabled (which it is by default).

In practical use, the $ is the big deal, as one often may want the various expansions it enables to (variables and more), while still preventing the shell from muddling most of the command line.

mattdm
  • 40,245
  • Is this form of quoting standard across shells, or is it bash-specific? – apoorv020 Feb 17 '11 at 14:37
  • It is bash-specific. Plain /bin/sh bourne shell is similar, but bash can do more things so it works slightly differently. Same with ksh, which is also mostly kinda bourne-like. Tcsh and other csh-style shells work differently. – mattdm Feb 17 '11 at 14:40
  • @apoorv020: This is common to all Bourne-style shells (Bourne, POSIX, ash, ksh, bash, zsh, …), apart from the treatment of ! as a history character which is specific to bash. Csh and fish have different rules. – Gilles 'SO- stop being evil' Feb 17 '11 at 17:56
  • The parameters * and @ get special treatment inside double quotes, but this means that "$*" and "$@" are special (actually only "$@" is special; $* and $@ are special when unquoted) . Something like "@*" is perfectly ordinary, just a literal two-character string. – Gilles 'SO- stop being evil' Feb 17 '11 at 17:59
2

While single quotes preserve the literal value of all characters they enclose,

double quotes differ in that they do not preserve the literal value of the dollar sign , $, the back-ticks, - - , and the backslash , .

When enclosed with double quotes, the dollar sign and back-ticks preserve their special meaning , and the special meaning of the backslash character is only retained when it precedes a dollar sign , back-tick , double quote, backslash , or newline.

Example:

[user@localhost~]$ echo '$HOME'
$HOME
[user@localhost~]$ echo '`pwd`'
`pwd`
[user@localhost~]$ echo '"Hello world"'
"Hello world"

[user@localhost~]$ echo "$HOME"
/home/user
[user@localhost~]$ echo "`pwd`"
/home/user

[user@localhost~]$ echo ""Hello world""
Hello world
[user@localhost~]$ echo "\$HOME"
$HOME
[user@localhost~]$ echo "\`pwd\`"
`pwd`
[user@localhost~]$ echo "\"Hello ,world\""
"Hello, world"
Rakib
  • 2,435