UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 13.7 The () Subshell Operators Chapter 13
Redirecting Input and Output
Next: 13.9 Send Output Two or More Places with tee
 

13.8 Using {list} to Group Bourne Shell Commands

A lot of people know that you can group the output of a series of commands by using a subshell (13.7). That is, instead of this:

$ date > log
$ who >> log
$ ls >> log

they start a subshell with parentheses:


> 
$ (date
> who
> ls) > log

and only redirect once to log. But a subshell takes an extra process and takes time to start on a busy system. If all you need to do is redirect output (or input) of a set of commands, use the Bourne shell's list operators {} (curly braces):

$ { date
> who
> ls
> } > log

Notice the spaces and the extra RETURN at the end. Each command must be separated from others. You can also write (note the semicolon after the last command):

$ { date; who; ls; } > log

Here are two other differences between the subshell (parentheses) and list (curly braces) operators. A cd command in the subshell doesn't change the parent shell's current directory; it does in a list. Also, a variable set in a subshell isn't passed to the parent shell; from a list, the variable is passed out.

NOTE: Jonathan I. Kamens points out that some Bourne shells may run a list in a subshell anyway, especially if there's a pipe involved. If your Bourne shell works like the example shown here, it's using a subshell, too:

$ { echo frep; foo=bar; } | cat
frep
$ echo $foo

$ { echo frep; foo=bar; }

frep
$ echo $foo
bar

- JP


Previous: 13.7 The () Subshell Operators UNIX Power ToolsNext: 13.9 Send Output Two or More Places with tee
13.7 The () Subshell Operators Book Index13.9 Send Output Two or More Places with tee

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