Differences between doublequotes " ", singlequotes ' ' and backticks ` ` on commandline?
For the sake of example, consider that variable foo
contains uname
(foo=uname
).
echo "$foo"
outputsuname
, substituting variables in text.- For a literal
$
character inside"
quotes, use\$
; for a literal"
, use\"
.
- For a literal
echo '$foo'
outputs$foo
, the exact string.- Even
'
can't be escaped as\'
inside'
quotes. But you can use'foo'\''bar'
.
- Even
echo `$foo`
outputsLinux
, executing the content of the variable andecho
printing it.
This is explained very nicely in the relevant section of the bash manual. Briefly, anything within single quotes is interpreted literally. So, for example:
$ echo '$SHELL'
$SHELL
$ echo '{1..3}'
{1..3}
Compare that to the unquoted versions:
$ echo $SHELL
/bin/bash
$ echo {1..3}
1 2 3
Double quotes allow variable expansion (also history expansion and some other things). Basically, you use them when you are dealing with something that you want to see expanded. For example:
$ echo "$SHELL"
/bin/bash
$ echo "!!"
echo "echo "$SHELL""
echo /bin/bash
In other words, single quotes completely protect a string from the shell while double quotes protect some things (spaces for example) but allow variables and special characters to be expanded/interpreted correctly.
Single quotes (''
) are used to preserve the literal value of each character enclosed within the quotes.
Using double quotes (""
), the literal value of all characters enclosed is preserved, except for the dollar sign ($
), the backticks (backward single quotes, ``
) and the backslash (\
).
When enclosed inside back-ticks (``
), the shell interprets something to mean "the output of the command inside the back-ticks." This is referred to as "command substitution", as the output of the command is substituted for the command itself.
references:
- http://www.linuxtopia.org/online_books/bash_guide_for_beginners/sect_03_03.html
- http://www.linux-tutorial.info/modules.php?name=MContent&pageid=20