The problem with backslashes is that many different programs use them
as quoting characters. As a result, it's difficult to figure out how
many backslashes you need in any situation.
Here's an example, taken from System V Release 4.
(Notice that I'm using the standard System V version of echo
SVR4 has four versions of echo
/bin/echo hi \ there
/bin/echo hi \\ there
hi \ there
/bin/echo hi \\\\ there
hi \ there
In the first case, the shell uses the backslash to
the following space character.
The space before the backslash is a word separator.
gets two arguments: "
" 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.
command gets three arguments,
" and "
", 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
interprets backslashes (8.6
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
this; you'd see two backslashes.
For that matter, if you're using
SVR4's C shell, with its built-in echo
see the BSD behavior.
You'll also see the BSD behavior if you're
using SVR4's /usr/ucb/echo
terminal driver (42.1
is 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!
What happens when you put quotes (
) around the
strings in the echo
Especially, should quotes affect the way that the