Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I used to use '' and "" 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?

share|improve this question
    
Have a look on to this question in StackExchange's AskUbuntu. –  shellholic Feb 17 '11 at 14:52
    
A great example of why it's counter-productive for there to be an ubuntu-specific site. –  mattdm Feb 17 '11 at 15:50
    
@mattdm I totally agree –  shellholic Feb 17 '11 at 16:06

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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