7

This question already has an answer here:

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?

marked as duplicate by Gilles bash Feb 26 at 12:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Have a look on to this question in StackExchange's AskUbuntu. – shellholic Feb 17 '11 at 14:52
12

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.

  • 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 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 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"

Not the answer you're looking for? Browse other questions tagged or ask your own question.