UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 9.12 The Bourne Shell for Loop Chapter 9
Saving Time on the Command Line
Next: 9.14 Using Here Documents for Form Letters, etc.
 

9.13 Multiline Commands, Secondary Prompts

Both the Bourne shell and the C shell support multiline commands. In the Bourne shell, a newline following an open quote (' or "), a pipe symbol (|), or a backslash (\) will not cause the command to be executed. Instead, you'll get a secondary prompt (from the PS2 environment variable, set to > by default) and you can continue the command on the next line. For example, to send a quick write (1.33) message without making the other user wait for you to type the message:

$ echo "We're leaving in 10 minutes. See you downstairs." |
> write joanne

In the C shell, you can continue a line by typing a backslash (\) before the newline (8.15). You won't get the secondary prompt.

Obviously, this is a convenience if you're typing a long command line. It is a minor feature and one easily overlooked; however, it makes it much easier to use a program like sed (34.24) from the command line. For example, if you know you chronically make the typos "mvoe" (for "move") and "thier" (for "their"), you might be inspired to type the following command:


nroff lp 
$ sed '
> s/mvoe/move/g
> s/thier/their/g' myfile | nroff -ms | lp

More importantly, the ability to issue multiline commands lets you use the shell's programming features interactively from the command line. In both the Bourne and the C shell, multiline programming constructs automatically generate a secondary prompt (> in the Bourne shell, ? in the C shell) until the construct is completed.

For example, here's a place to use my favorite programming construct for non-programmers, the for loop (9.12):

$ for x in file1 file2 file3
> do
> sed 's/thier/their/g' $x > ,$x
> mv ,$x $x
> done
$

Or in the C shell with foreach (9.11):

% foreach x (file1 file2 file3)
? sed 's/thier/their/g' $x > ,$x
? mv ,$x $x
? end
%

While a simple command like this could be saved into a shell script (1.5), it is often even easier to use it interactively.

Users of sed should of course makesure their script works correctly before overwritingtheir original file . (34.3)

- TOR


Previous: 9.12 The Bourne Shell for Loop UNIX Power ToolsNext: 9.14 Using Here Documents for Form Letters, etc.
9.12 The Bourne Shell for Loop Book Index9.14 Using Here Documents for Form Letters, etc.

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