LinuxDevCenter.com

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things Linux across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to Linux Subscribe to Newsletters

Unix Power Tools
The () Subshell Operators

by Tim O'Reilly and Jerry Peek
01/27/2000

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

Combining Several Commands

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

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

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

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) &

Temporary Change of Directory and Environment

The parentheses start a subshell. 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 :

% 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.


* 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 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.


Back More Unix Power Tools

 




Tagged Articles

Be the first to post this article to del.icio.us

Recommended for You

  1. Cover of Learning the vi and Vim Editors
    Learning the vi and Vim Editors
    Print: $34.99
    Ebook: $27.99
  2. Cover of Official GNOME 2 Developer's Guide
    Official GNOME 2 Developer's Guide
    Print: $49.95
  3. Cover of sed, awk and Regular Expressions Pocket Reference
    sed, awk and Regular Expressions Pocket Reference
    Print: $9.95
  4. Cover of Python for Unix and Linux System Administration
    Python for Unix and Linux System Administration
    Print: $49.99
    Ebook: $39.99

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com