3

I am trying to figure out what is the difference between " " and ' '.

When I use them with echo they both give the same result,

sps@sps-Inspiron-N5110:~$ echo Hello    world
Hello world
sps@sps-Inspiron-N5110:~$ echo "Hello   world"
Hello   world
sps@sps-Inspiron-N5110:~$ echo 'Hello   world'
Hello   world
sps@sps-Inspiron-N5110:~$ 

Is there any specific situation where we need to use one or the other ?

Thanks.

  • 1
  • @bodhi.zazen Thanks for the help. I should have searched in stackoverflow as well once before posting the question here. – sps Apr 4 '15 at 22:48
  • 1
    echo -e 'single quotes $HOME \n'"\ndouble quotes $HOME" – Panther Apr 4 '15 at 22:50
  • To reviewers considering closure: I think bash scripting is on-topic on Ask Ubuntu; plus, this question is of value. The bash reference manual is a great resource, that SO post is quite valuable too, and both make clear which syntactic constructions get expanded inside " quotes. But they require looking through multiple sections of docs to figure out what semantic constructions (i.e., specifically what kinds of expansions) are usable in "...". So I've tried to answer, from that perspective. – Eliah Kagan Apr 5 '15 at 1:25
5

In bash (Ubuntu's default interactive shell), everything following an open single quote (') is quoted literally, up until the next ' which is taken as the closing quote. In contrast, several kinds of shell expansions are performed inside double quotes (").

  • Parameter expansion (with $... or ${...}):

    ek@Io:~$ echo '$HOME ${USER^^*} ${SHELL%%+([[:alnum:]])}'
    $HOME ${USER^^*} ${SHELL%%+([[:alnum:]])}
    ek@Io:~$ echo "$HOME ${USER^^*} ${SHELL%%+([[:alnum:]])}"
    /home/ek EK /bin/
    

    This is a common use case for " quotes in shell scripting. In particular, you may have seen loops that iterate through filenames, expanding a variable inside double quotes so it does get expanded but doesn't have its value split into separate words. For example, a command like this might help me keep track of things while preparing lots of media files for moving:

    ek@Io:~$ for d in to\ *; do ls -ld "$d"; du -sh "$d"; done
    drwxr-x--- 856 ek ek 32768 Mar 29 10:47 to Albums (partial)
    308G    to Albums (partial)
    drwxrwxr-x 1191 ek ek 40960 Mar 11 12:56 to Books
    21G to Books
    
  • Command substitution (with $(...) or `...`) and arithmetic expansion (with $((...))):

    ek@Io:~$ echo '$(date)      `uname -r`      $((2+2))'
    $(date)      `uname -r`      $((2+2))
    ek@Io:~$ echo "$(date)      `uname -r`      $((2+2))"
    Sat Apr  4 20:42:37 EDT 2015      3.19.0-11-generic      4
    
  • Escaping single characters with \ is supported and can be handy in " quotes, but is unsupported (and would generally be pointless) in the simpler ' quotes:

    ek@Io:~$ echo '$HOME \$HOME \\\\'
    $HOME \$HOME \\\\
    ek@Io:~$ echo "$HOME \$HOME \\\\"
    /home/ek $HOME \\
    
  • History expansion with ! (if enabled, which it usually is when you're using the shell interactively):

    ek@Io:~$ # this is just a comment (but I could run a command here)
    ek@Io:~$ echo '!!'
    !!
    ek@Io:~$ # this is just another comment (but could be another command)
    ek@Io:~$ echo "!!"
    echo "# this is just another comment (but could be another command)"
    # this is just another comment (but could be another command)
    

In addition, one of the most common reasons to use double quotes are to easily and intuitively quote text containing single quotes (since a ' has no special meaning at all, inside " quotes):

ek@Io:~$ echo "We'll have the dog's fleas gone soon, you'll see."
We'll have the dog's fleas gone soon, you'll see.

Finally, though you cannot escape a ' inside '-quoted text, you can achieve the same result by ending quoting with ', using an escaped ' (\'), and resuming quoting with ':

ek@Io:~$ echo '"My $PATH ain'\''t what it used to be," said the stevedore'\''s apprentice.'
"My $PATH ain't what it used to be," said the stevedore's apprentice.
  • Thanks for the elaborated explaination. I have also changed the question by renaming 'Linux' to 'bash' to make it more correct question. – sps Apr 5 '15 at 1:23
0

stackoverflow link posted above by bodhi.zazen answers this question.

Below is one situtation where it makes difference.

sps@sps-Inspiron-N5110:~$ echo "Hello, I have only $((3+5)) $ in my account"
Hello, I have only 8 $ in my account
sps@sps-Inspiron-N5110:~$ echo 'Hello, I have only $((3+5)) $ in my account'
Hello, I have only $((3+5)) $ in my account
sps@sps-Inspiron-N5110:~$ 

Posting this answer, just to let others know that I've got the answer already. (I had posted this question .)

Thanks.

  • depending on what you are doing , you may wish to look at printf – Panther Apr 4 '15 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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