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

Unix Power ToolsUnix Power ToolsSearch this book

43.7. The ( ) Subshell Operators

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

43.7.1. Combining Several Commands

The parentheses start a subshell (Section 24.4) that, in effect, "collects" the output of all the commands inside. (It does the same thing for the standard input and standard error.) The output of the entire group can be passed together into a single pipeline. For example:

echo Section 27.5

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

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

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

Parentheses are also useful in the Bourne shell if you want to put an entire sequence of commands separated by semicolons (;) (Section 28.16) 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 apply only 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) &

43.7.2. Temporary Change of Directory and Environment

Commands that run between the parentheses won't affect the parent shell's environment. For instance, to run a command in another directory without changing your active shell's current directory (Section 24.3):

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

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

--TOR and JP

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.