home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 8.19 "Special" Characters and Operators Chapter 8
How the Shell Interprets What You Type
Next: 9. Saving Time on the Command Line
 

8.20 How Many Backslashes?

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 from /bin/echo . SVR4 has four versions of echo !)

% /bin/echo hi \ there


hi  there
% /bin/echo hi \\ there


hi \ there
% /bin/echo hi \\\\ there


hi \ there

In the first case, the shell uses the backslash to quote (8.14 ) the following space character. The space before the backslash is a word separator. So echo gets two arguments: "hi " and " there " (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 \\ to \ ; the first backslash tells the shell to quote (8.14 ) (turn off the special meaning of) the second backslash. The echo command gets three arguments, "hi ", "\ " 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 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 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 .

The 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:

% echo \

[CTRL-u]

The shell receives the line echo CTRL-u . 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 troff (43.13 ) (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!

(Extra credit: What happens when you put quotes (" or ' ) around the strings in the echo commands above? Especially, should quotes affect the way that the \ [CTRL-u] is interpreted?)

- ML , JP


Previous: 8.19 "Special" Characters and Operators UNIX Power Tools Next: 9. Saving Time on the Command Line
8.19 "Special" Characters and Operators Book Index 9. Saving Time on the Command Line

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System