Here's an example, taken from System V Release 4. (Notice that I'm using the standard System V version of echo from /bin/echo. SVR4 has four versions of echo!)
/bin/echo hi \ therehi there %
/bin/echo hi \\ therehi \ there %
/bin/echo hi \\\\ therehi \ there
In the first case, the shell uses the backslash to
hi" and "
(without the quotes)-where
is the space character
that was quoted by the backslash.
As always, echo prints a single space between each argument.
The first space you see in the output is echo's argument-separating
space, and the second space came along with the second argument
(thanks to the backslash).
In the second case, the shell converts
the first backslash tells the shell to
(turn off the special meaning of) the second backslash.
The echo command gets three arguments,
\" and "
there", and it
echoes those arguments with a single space between each.
(I've heard claims
that, on some systems, this command wouldn't print any backslashes,
but I wasn't able to reconstruct that situation.)
In the third case, the shell converts each pair of backslashes into a
backslash, and runs the command
echo hi \\ there.
But this is
System V, and System V's echo
as special characters.
So when echo sees the remaining two backslashes, it converts
them into a single backslash. So you only see a single backslash,
even though you typed four. On BSD systems, echo doesn't do
this; you'd see two backslashes.
For that matter, if you're using
SVR4's C shell, with its built-in echo command, you'll
see the BSD behavior.
You'll also see the BSD behavior if you're
using SVR4's /usr/ucb/echo.
Theis also capable of "eating" backslashes if they appear before special characters. If a backslash precedes the "erase" character (normally CTRL-h) or the "kill" character (normally CTRL-u), the terminal driver will pass the control character to the shell, rather than interpreting it as an editing character. In the process, it "eats" the backslash. So if you type:
The shell receives the line
See the termio manual page for more information; there are
certainly system-dependent variations.
What's the point of this article? Well, backslashes are messy. The shell, the terminal driver, echo (sometimes), and several other utilities use them. If you think very carefully, you can figure out exactly what's consuming them. If you're not of a rigorous frame of mind, you can just add backslashes until you get what you want. (But, obviously, the non-rigorous approach has pitfalls.) I've seen situations in(which is another story altogether) where you need eight backslashes in order to have a single backslash left at the point where you want it!