Backslash is often used to say “make the next character stand for itself instead of giving it a special meaning”. We say that backslash quotes the next character. This meaning is present in many programming languages, including shell syntax. For example, in rpm-qa rpmname\*
, the backslash causes the *
character to be passed in the argument to the rpm-qa
command. In contrast, rpm-qa rpmname*
would replace rpmname*
by the list of file names in the current directory starting with rpmname
. (If there is no matching file then bash leaves rpmname*
alone; zsh would by default complain that there are no matches.)
The shell offers another way to interpret a character literally, i.e. to make it stand for itself rather than be interpreted in some special way. That's quoting, which cancels the special meaning of a whole sequence of characters. There are two kinds of quotes: single quotes '
make every character stand for itself until the next single quote, whereas double quotes "
conserve a special meaning for a few characters ("
, $
, \
and `
). In echo 'that'\''s no moon'
, the echo
command receives a single argument, which is the concatenation of that
(written with a literal string, not that there was any character with a special meaning in that particular case), '
(backslash-single quote cancels the special meaning of that single quote, so this expands to one single quote character), and s no moon
(the spaces lose their special meaning since they're within quotes: instead of separating arguments, they're part of the argument).
In the shell, when backslash has a special meaning, that's usually to quote the next character. But:
- Inside single quotes, backslash has no special meaning.
- Inside double quotes, backslash only quotes the next character if it's one of
"$\`
. Otherwise both the backslash and the following character are interpreted literally, e.g. "\a"
is the two-character string \a
.
- Inside dollar-single-quote literals, backslash has a different meaning similar to the one in C. A backslash followed by octal digits or by certain letters provides alternate ways of entering characters, which is useful for unprintable characters. For example,
\n
is a lowercase N, "\n"
is backslash+n, but $'\n'
is a newline character.
In grep '\s/tmp' /etc/fstab
, the single quotes cause grep
to receive the argument \s/tmp
. This argument is a regular expression. There are many syntax variants for regular expressions, but most based on one of two standards: POSIX basic regular expressions (BRE) and extended regular expressions (ERE). ERE follow the widespread convention that backslash followed by anything other than a letter or digit quotes the following character. But for historical reasons, in BRE, a backslash can sometimes make the next character special when it wouldn't otherwise be. In this case, \s
is a GNU grep extension to the basic regex syntax, available both with BRE and with ERE, meaning one whitespace character. Thus grep '\s/tmp' /etc/fstab
lists lines in /etc/fstab
that contain /tmp
preceded by a space or tab.