Ubuntu – Difference between “ ” and ‘ ‘ in bash

command lineecho

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.

Best Answer

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.
Related Question