9

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
  • 133,243
  • 15
  • 236
  • 397
apoorv020
  • 1,243
  • 3
  • 13
  • 19
  • Have a look on to this [question](http://askubuntu.com/questions/20034/differences-between-doublequotes-singlequotes-and-backticks-on-comma) in StackExchange's AskUbuntu. – shellholic Feb 17 '11 at 14:52

2 Answers2

13

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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
mattdm
  • 39,535
  • 18
  • 99
  • 133
  • 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
1

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,347
  • 1
  • 17
  • 20