Double quotes
Single quotes
Shell escape character
Quoting just part of a word
Wildcarding with special characters
See also
The shell has several quoting mechanisms for marking a section of a command for special processing. One of them, command substitution, which uses the `...` or ``...`` syntax, is discussed in the chapter on i/o redirection.
The other quoting mechanisms focus more simply on the problem of overriding the special meanings that certain characters have.
Double quotes are of use when you want to pass a character string containing a space or other word separator to an application. Normally, if you called a C program, it would see these words as separate argv entries instead of a single character string; double quotes prevents this breakup into words. We can demonstrate this using the simple myecho program in the Samples directory which prints out the argv list it receives with single quotes around each entry:
Double quotes also turn off the special meaning of the various wildcard characters and the single quote:
Command, history and variable substitutions inside double quotes are still done:
Single quotes are a little more brute force way to turn off special meanings. Wildcards, variables and command substitutions are all treated as ordinary text. Only history references are recognized inside single quotes.
It is not necessary to quote an entire word. It’s possible (and often useful) to selectively quote just as much as desired. The quoting characters are processed out just before invoking the command. Example:
The shell escape character is normally the circumflex, ^. It has two uses: preceding any of the special characters, it turns off that special meaning. When followed by an alphabetic character or hex or octal number, it can be used to enter binary data or characters that couldn’t easily be typed. To get a literal escape character, type two escapes in a row. These specific escape sequences have special meaning:
^a | Audible Alert (Bell) |
^r | Carriage Return |
^b | BackSpace |
^t | Tab |
^f | Form Feed |
^v | Vertical Tab |
^n | NewLine |
^^ | Single escapesym |
At the very end of a line, the escape has a special meaning: the next line is a continuation line. Inside a quoted string, the ^-newline combination will be replaced with a simple newline; anywhere else, the combination is just turned into a space. The other special case is when it immediately follows[. Since [^...] is a wildcard exclusion range, the ^ in this case is treated as a literal character so you won’t have to type two of them in a row.
Escape characters work even inside single or double quotes.
It’s possible to combine the quoting mechanisms or use them on just the part of a string you want quoted. For example,
Here’s another example, searching through a series of .csh files, looking for those that are self-loading procedures. For example, we can spot that whereis.csh is self-loading because it contains a line like this:
To look for occurrences of this sort, we might loop through a list of .csh files, grep’ing each for the filename (minus the directory and .csh extension) followed by white space followed by $argv:
Notice how the $i:b part is outside the quotes so that the filename can be substituted in and edited to strip off the directory and extension. Conversely, the [ ^t]* and $argv portions are inside the quotes to avoid having them confused as a wildcard or variable substitution, respectively.
If you’d like to wildcard filenames that have literal $, [, quote or other special characters, you’ll have to quote or escape the special characters to turn off their special meanings. For example,
Quoting
History recall
Variable substitution
Order of evaluation
Tutorial: The history mechanism
Tutorial: Variable substitutions
Tutorial: Command substitution