start page | rating of books | rating of authors | reviews | copyrights

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 13.6 Safe I/O Redirection with noclobber Chapter 13
Redirecting Input and Output
Next: 13.8 Using {list} to Group Bourne Shell Commands
 

13.7 The () Subshell Operators

A useful shell trick is to use parentheses, (), to group commands.

13.7.1 Combining Several Commands

The output of the entire group can be passed together into a single pipeline. For example:

echo 
$ (cat file1; echo .bp; cat file2) | nroff

This will interpose the nroff (43.13) .bp (break page) request between two files to be formatted. [3]

[3] If you're using only cat and a single echo, you can use this command instead:

$ echo .bp | cat file1 - file2 | nroff

The cat \- option (13.13) tells cat to read its standard input (in this case, from the pipe and the echo) at that point. nroff gets exactly the same input.

Parentheses are also very useful in the Bourne shell if you want to put an entire sequence of commands separated by semicolons into the background. In the C shell, the command line below will go immediately into the background.

& 
% nroff -ms file1; nroff -ms file2 &

But in the Bourne shell, the background request (&) will only apply to the second command, forcing you to wait for completion of the first job before you get back the system prompt. To get right back to work, you can type:

$ (nroff -ms file1; nroff -ms file2) &

13.7.2 Temporary Change of Directory and Environment

The parentheses start a subshell (38.4). Commands that run between the parentheses won't affect the parent shell's environment. For instance, to run a command in another directory without changingyour active shell's current directory : (38.3)

% pwd
/home/trent
% (cd somewhere-else; nroff -ms file1 > file.out) &
[1] 22670
% pwd
/home/trent

The file file.out will be created in the somewhere-else directory.

Article 13.8 shows another method for the Bourne shell. It's more limited but can also be more efficient.

- TOR, JP


Previous: 13.6 Safe I/O Redirection with noclobber UNIX Power ToolsNext: 13.8 Using {list} to Group Bourne Shell Commands
13.6 Safe I/O Redirection with noclobber Book Index13.8 Using {list} to Group Bourne Shell Commands

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