Single quotes enclose a value which is to be taken literally: all types of expansion are suppressed. Usually used if the value includes or may include white space (space, tab, new-line), or special characters ($
, \
, `
) that the user does not want to be expanded/treated specially by the shell.
Double quotes enclose a value which will have variables, and character replacement done. Required when the output may contain whitespace, and must be assigned as a single value.
Back quotes enclose a command, the results of which are wanted as value. Newer shells allow the use of $(...)
in place of `...`
. I prefer the newer method.
The following code may help understand what is happening.
CMD='ls .'
echo sq: $CMD
set $CMD
echo raw 1: $1
echo raw: $*
set "$CMD"
echo dq: $1
echo bq: `$CMD`
echo new: $($CMD)
\
is always treated literally when enclosed in single quotes, even if it precedes a'
. After an opening'
, the next'
is always interpreted as the closing quote. Something like quoting for'
in a single-quoted string can be achieved by ending quoting, escaping'
individually, and starting quoting again. That is, while\'
doesn't work between opening and closing'
characters,'\''
does. (See Gilles's correction to one of my posts when I'd made the same mistake for details.) – Eliah Kagan Oct 15 '14 at 20:04