Quoting
Quoting is how you prevent the shell from interpreting things
differently from what you want it to. For example, if you want a command
to receive an argument containing metacharacters, such as *
or ?
, you
have to quote the metacharacters. Or, quite typically, when you want to
keep something as a single argument that the shell would otherwise treat
as separate arguments, you have to quote the arguments. There are three
ways to quote things:
- Backslash escaping
Preceding a character with a backslash (
\
) tells the shell to treat that character literally. This is the easiest way to quote a single character:
$ echo here is a real star: \* and a real question mark: \?
here is a real star: * and a real question mark: ?
- Single quotes
Single quotes ('...') force the shell to treat everything between the pair of quotes literally. The shell strips the two quotes, and otherwise leaves the enclosed text completely alone:
$ echo 'here are some metacharacters: * ? [abc] ` $ \'
here are some metacharacters: * ? [abc] ` $ \
There is no way to embed a single quote within a single-quoted
string. Even backslash is not special within single quotes. (On some
systems, a command like echo 'A\tB
'
makes it look like the shell treats backslash specially. However, it is
the echo command doing the special
treatment: see Table 2-2
for more information.)
If you need to mix single and double quotes, you can do so by careful use of backslash escaping and concatenation of differently quoted strings:
$ echo 'He ...
Get Classic Shell Scripting now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.