Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

6 Answers 6

up vote 19 down vote accepted

For example purpose, consider that variable foo contains uname.

  • echo "$foo" outputs uname, substituting variables in text.
    (For a literal $ character inside " quotes, use \$; for a literal ", use \".)
  • echo '$foo' outputs $foo, the exact string.
    (Even ' can't be escaped as \' inside ' quotes. But you can use '\''.)
  • echo `$foo` outputs Linux, execute the content of the variable and echo outputs it.
share|improve this answer
2  
@Stefano Palazzo thanks, better command (I used ls -ad ., uname is way better) –  shellholic Jan 4 '11 at 17:03
    
\ 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 at 20:04

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 inside the back-ticks 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

share|improve this answer

Am just talking in general but:

"" - Used to process some info inside of them before output. Like in PHP when you " " a variable For example: echo "Hello my name is $name" (Will output: Hello my name is NESS)

' ' - Used to show the ouput as it is. For example in PHP using the example above it will output the echo as it is (Will output: Hello my name is $name)

So the difference between Double and Single is that Double Quotes process the variable and show the value while single qoutes show the name of the variable without processing.

` ` - Used to quote commands that are later assign to another variable.

share|improve this answer

Single quotes enclose a value which is to be taken literally. Usually used if the value includes or may include white space (space, tab, new-line), or special characters ($, ).

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)
share|improve this answer
echo '$HOME' is "$HOME"

Single quotes prevent the variable expansion, while double quotes allow it.

share|improve this answer

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.

share|improve this answer

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.