I often see tutorials on the web or posts on this site which make heavy use of the following characters at the command line. Often it seems that they are used for pretty similar purposes. What are the differences between them when used on the command line or for shell programming? For what purpose do I use which of them?
" " double quotes
' ' single quotes
` ` backticks
For the sake of example, consider that variable
foo
containsuname
(foo=uname
).echo "$foo"
outputsuname
, substituting variables in text.$
character inside"
quotes, use\$
; for a literal"
, use\"
.echo '$foo'
outputs$foo
, the exact string.'
can't be escaped as\'
inside'
quotes. But you can use'foo'\''bar'
.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:
Compare that to the unquoted versions:
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:
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:
Single quotes prevent the variable expansion, while double quotes allow it.
Am just talking in general but:
""
- Used to process some info inside of them before output. (Like in PHP.)name=NESS; echo "Hello my name is $name"
will output:Hello my name is NESS
''
- Used to show the output as it is. (Also like in PHP.)Hello my name is $name
So the difference between double and single quotes is that double quotes process the variable and show the value, while single quotes show the name of the variable without processing.
``
- Used to quote commands that are later assigned to another variable.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.