Quoting

As we’ve seen, each word in a command is separated by whitespace. What if you want to designate a shell “word” that contains whitespace? The shell offers three methods of quoting text, which we will discuss below. Aside from the <space> character, the following characters must always be quoted if they are meant to be taken literally, because they have special meaning to the shell:

| & ; < > ( ) $ ` \ " ' <space> <tab> <newline>

A few other characters can sometimes have special meaning depending on their context, and may also sometimes need to be quoted to remove that special meaning:

* ? [ # ~ = %

When in doubt, quote it out!

Backslash Escapes

Any single character can be quoted by inserting a backslash in front of it. With our earlier “Hello World!” command, we could join the two words into one word like so:

$ echo Hello\ World!
Hello World!

Well, it looks pretty much the same because the echo utility adds a space between its arguments when printing them out, so quoting a single space between two arguments won’t change the output. However, multiple spaces can illustrate the difference:

$ echo Hello    World!
Hello World!
$ echo Hello\ \ \ \ World!
Hello    World!

And, of course, if you want a literal backslash, you just need two:

$ echo C:\\Program\ Files\\...wait, this is UNIX!
C:\Program Files\...wait, this is UNIX!

One other feature, which is more useful for shell scripts, is that a backslash at the end of a line acts as a line-continuation; the backslash and the newline character following it are completely ignored. This is often used to wrap long commands by placing individual components on separate lines, as shown below:

$ echo Hello \
> World!
Hello World!

Notice the `> ` secondary shell prompt which is printed before successive lines of input when a command spans multiple lines. Backslash escapes are pretty handy for escaping a few stray characters, but what about when you want to do a whole string of them?

Single Quotes

Single quotes are pretty straightforward. Every character between single quotes is taken literally:

$ echo 'Hello    World!'
Hello    World!

You can quote parts of a word, too:

$ echo H'e''llo  ''  World!'
Hello    World!

Single quotes are useful, but awfully annoying if you want a literal single quote in the middle of a single-quoted string. Single quotes don’t nest, and there is no way to escape a single quote within a single-quoted string, since an embedded backslash loses its special meaning. Instead, you have to terminate the quoting, escape a single quote, and then continue with the quoting:

$ echo 'It'\''s such a beautiful day!'
It's such a beautiful day!

Double Quotes

Double quotes work just like single quotes, except that they use the " character, and three special characters retain their meaning:

  • The dollar sign, $, which is used for expansions.

  • The backquote (or backtick) `, which is used for command substitution.

  • The backslash, \, which we mentioned above–for escaping characters.

We will discuss the meaning of the dollar sign and backquote in a moment when we go over expansions. Since the backslash retains its meaning, double quotes can be nested by escaping embedded quotes:

$ echo "And I said, \"Hello World!\""
And I said, "Hello World!"