Quoting special characters

Certain characters may have special meaning to the shell, depending on their context. If you want a command line to include any of the special characters that the shell processes, then you may have to quote these characters to force the shell to treat them as simple characters.

You must quote the following characters to avoid their special interpretation:

| $ ( " ) & ` ; \ ' Tab Newline Space

You might need to quote the following characters, depending on their context within a shell command:

* ? [ # ~ = %

In order to quote: You can:
A single character Precede the character with a single backslash (\) character
All special characters within a string of characters Enclose the whole string in single quotes
All special characters within a string, except for $, `, and \ Enclose the whole string in double quotes

For example, these commands search for all occurrences of the string “realtime OS” in the chapter1.html file:

grep realtime\ OS chapter1.html
grep 'realtime OS' chapter1.html
grep "realtime OS" chapter1.html

However, note that:

grep realtime OS chapter1.html

doesn't do what you might expect, as it attempts to find the string “realtime” in the files named OS and chapter1.html.

Depending on the complexity of a command, you might have to nest the quoting. For example:

find -name "*.html" | xargs grep -l '"realtime.*OS"' | less

This command lists all the HTML files that contain a string consisting of realtime, followed by any characters, followed by OS. The command line uses find to locate all of the files with an extension of html and passes the list of files to the xargs command, which executes the given grep command on each file in turn. All of the output from xargs is then passed to less, which displays the output, one screenful at a time.

This command uses quoting in various ways to control when the special characters are processed, and by which process:

For more information, see Quoting in the entry for ksh in the Utilities Reference.