Quotation of characters in the Korn shell or POSIX shell

When you want the Korn shell or POSIX shell to read a character as a regular character, rather than with any normally associated meaning, you must quote it.

Each metacharacter has a special meaning to the shell and, unless quoted, causes termination of a word. The following characters are considered metacharacters by the Korn shell or POSIX shell and must be quoted if they are to represent themselves:

  • pipe (|)
  • ampersand (&)
  • semicolon (;)
  • less-than sign (<) and greater-than sign (>)
  • left parenthesis (() and right parenthesis ())
  • dollar sign ($)
  • backquote (`) and single quotation mark (')
  • backslash (\)
  • double-quotation marks (")
  • newline character
  • space character
  • tab character

To negate the special meaning of a metacharacter, use one of the quoting mechanisms in the following list.


Item Description
Backslash A backslash (\) that is not quoted preserves the literal value of the following character, with the exception of a newline character. If a newline character follows the backslash, then the shell interprets this as line continuation.
Single Quotation Marks Enclosing characters in single quotation marks (' ') preserves the literal value of each character within the single quotation marks. A single quotation mark cannot occur within single quotation marks.

A backslash cannot be used to escape a single quotation mark in a string that is set in single quotation marks. An embedded quotation mark can be created by writing, for example: 'a'\''b', which yields a'b.

Double Quotation Marks Enclosing characters in double quotation marks (" ") preserves the literal value of all characters within the double quotation marks, with the exception of the dollar sign, backquote, and backslash characters, as follows:
$
The dollar sign retains its special meaning introducing parameter expansion, a form of command substitution, and arithmetic expansion.

The input characters within the quoted string that are also enclosed between $( and the matching ) will not be affected by the double quotation marks, but define that command whose output replaces the $(...) when the word is expanded.

Within the string of characters from an enclosed ${ to the matching }, there must be an even number of unescaped double quotation marks or single quotation marks, if any. A preceding backslash character must be used to escape a literal { or }.

`
The backquote retains its special meaning introducing the other form of command substitution. The portion of the quoted string, from the initial backquote and the characters up to the next backquote that is not preceded by a backslash, defines that command whose output replaces ` ... ` when the word is expanded.
\
The backslash retains its special meaning as an escape character only when followed by one of the following characters: $, `, ", \, or a newline character.

A double quotation mark must be preceded by a backslash to be included within double quotation marks. When you use double quotation marks, if a backslash is immediately followed by a character that would be interpreted as having a special meaning, the backslash is deleted, and the subsequent character is taken literally. If a backslash does not precede a character that would have a special meaning, it is left in place unchanged, and the character immediately following it is also left unchanged. For example:
"\$"    ->      $
"\a"    ->      \a
The following conditions apply to metacharacters and quoting characters in the Korn or POSIX shell:
  • The meanings of dollar sign, asterisk ($*) and dollar sign, at symbol ($@) are identical when not quoted, when used as a parameter assignment value, or when used as a file name.
  • When used as a command argument, double quotation marks, dollar sign, asterisk, double quotation marks ("$*") is equivalent to "$1d$2d...", where d is the first character of the IFS parameter.
  • Double quotation marks, at symbol, asterisk, double quotation marks ("$@") are equivalent to "$1" "$2" ....
  • Inside backquotes (``), the backslash quotes the characters backslash (\), single quotation mark ('), and dollar sign ($). If the backquotes occur within double quotation marks (" "), the backslash also quotes the double quotation marks character.
  • Parameter and command substitution occurs inside double quotation marks (" ").
  • The special meaning of reserved words or aliases is removed by quoting any character of the reserved word. You cannot quote function names or built-in command names.