Quoting

The shell regards many characters as special. Sometimes it is necessary to pass strings containing those special characters into a program or script as arguments. To prevent the shell from interpreting a special character, its special meaning can be temporarily disabled by quoting the character. There are several ways to do this.

A single character can be quoted by preceding its name with a backslash. For example

$ rm The\ File

deletes the file named The File. The space character is part of the first argument to rm. The usual special meaning of space as a delimiter between arguments was disabled for that one character.

The newline character can also be quoted in this way. For example

$ rm The\
> File

deletes the file named The\nFile (using C notation for the newline character). The usual special meaning of the newline character as a command delimiter was disabled for that one character.

Notice that the shell used the secondary prompt to collect the next line of the command.

If many characters need to be quoted, it is easier to use the '...' matched single quotes. For example

$ rm '|<*>|'

deletes the file named |<*>|. The special meaning associated with '>', '<', '|', and '*' have been disabled within the single quotes.

The single quotes does not disable the special meaning of '\', however. For example

$ rm 'I\'m done'

deletes the file named I'm done. The special meaning of the backslash is still active and thus the special meaning of the single quote just before the 'm' is disabled. Only a backslash can disable the special meaning of a backslash.

Note that the single quotes does disable the special meaning of the newline character. Thus

$ echo 'This is a mult-line echo command.
> This stuff is still part of the argument to echo.
> The newline character is embeded in this argument.
> The output appears below.'
This is a mult-line echo command.
This stuff is still part of the argument to echo.
The newline character is embeded in this argument.
The output appears below.
$

Double quotes can be used instead of single quotes. However, double quotes do not disable the special meaning of $ nor of backslash. Since $ is still handled within double quoted strings, you can cause shell variable expansions to occur in quoted material. For example

$ NAME=Peter
$ rm "$NAME's File"

deletes the file named Peter's File. Note that the NAME shell variable is expanded. Note also that the single quote is not special inside a double quoted string.

Quoting is very important when using programs that process commands containing the very same characters treated in a special way by the shell. For example, the grep command expects its first argument to be a regular expression. Regular expressions use many unusual characters. Thus it is commonly necessary to quote the first argument to grep. It is often done even when not necessary just out of habit. For example

$ grep '^A.*Z$' afile.txt

will search afile.txt for all lines that start with an 'A' and end with a 'Z'. Note that '*' and '$' are not treated special by the shell because of the enclosing single quotes. Thus these characters are passed to grep as is.

$ grep 'Peter' afile.txt

will search afile.txt for all lines that contain "Peter." In this case, the quoting is not necessary. However, there is no harm in quoting a character that does not need quoting; it is sometimes done anyway.

It is also possible to place the standard output of a command onto the command line. This is done using back quotes. For example

$ cat afile.txt
user1 user2 user3 user4
$ mail `cat afile.txt`

When the shell sees the back quotes it will execute the contained text (in a subshell) and place the standard output of the command back onto the command line to replace the back quoted material. In the example above, the mail command gets run with user1... user4 as arguments.

When the replacement of text is done, newlines in the standard output of the command are replaced with spaces. Thus if the command produces multiline output, all the text on the various lines gets folded into a single command line.

Since the shell executes a subshell to process the command, there is no limit to what can be accomplished inside the back quotes. In particular I/O redirection can be done. For example

$ for DIR in `echo $PATH | sed 's/:/ /g'`
> do
>   echo $DIR
> done
$

The subshell expands $PATH, processes the I/O redirection and honors the single quotes. The resulting text forms a list of names over which the loop operates.

Command substitution can even be nested. This is possible because the backslash is still processed by the shell as a special character even inside back quoted text. However, the backslash is removed before the subshell sees the back quoted text. For example

$ prog1 `prog2 \`prog3 arg\``

Here a subshell is created and told to run the command

prog2 `prog3 arg`

Of course to run this command the subshell must create still another subshell to run

prog3 arg

Back quotes are still honored inside of double quotes, but not inside of single quotes. For example

$ h
Hello world
$ TEST="`h` I'm happy"
$ echo $TEST
Hello world I'm happy
$ TEST='`h` I\'m happy'
$ echo $TEST
`h` I'm happy
$

Note that in all the cases above TEST contains a single string. Consider

$ TEST=Hello world
$ echo $TEST
Hello
$ TEST="Hello world"
$ echo $TEST
Hello world

Without the quotes, the second word used in the definition of TEST is simply ignored.