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 QNX Neutrino in the chapter1.html file:
grep QNX\ Neutrino chapter1.html grep 'QNX Neutrino' chapter1.html grep "QNX Neutrino" chapter1.html
However, note that:
grep QNX Neutrino chapter1.html
doesn't do what you might expect, as it attempts to find the string QNX in the files named Neutrino 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 '"QNX.*Neutrino"' | less
This command lists all the HTML files that contain a string consisting of QNX, followed by any characters, followed by Neutrino. 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:
- If you don't put quotes around the *.html, the shell interprets the *, and passes to find the list of files in the current directory with an extension of html. If you quote the *.html, the shell passes the string as-is to find, which then uses it to match all of the files in this directory and below in the filesystem hierarchy with that extension.
- In a similar way, if you don't quote the QNX.*Neutrino string at all, the shell generates a list of files that match the pattern. Quoting it once ("QNX.*Neutrino") works for a single invocation of grep, but this example has the added complexity of the xargs command.
- The xargs command takes a command line as its argument, and
the shell interprets this command line for each item that's passed to
If you don't want the QNX.*Neutrino string to be interpreted
by the shell
at all, you need to
put nested quotes around the pattern that you want to pass to
xargs grep -l '"QNX.*Neutrino"'
- The quoting also indicates when you want to execute the
As given, the shell passes the output from all of the invocations
of xargs to less.
In contrast, this command:
find -name "*.html" | xargs 'grep -l "QNX.*Neutrino" | less'
passes the command:
grep -l "QNX.*Neutrino" | less
to xargs, which will have quite different results—if it works at all.
For more information, see Quoting in the entry for ksh in the Utilities Reference.