DirectNET

Data Center Management Solutions including UPS Systems, Data Center Cooling, KVM over IP & IP Power Strips, Server Racks and Server Rack accessories; KVM Switches and KVM Extenders; Rackmount Monitors and Rackmount Keyboards.


NAVIGATION
Home
Store
INSIDE MAC
Television Shows
Broadcast Shows
Daily News Shows
Special Shows
EVENTS
DAILY TIPS
Design
Mac OS X
Mac OS X UNIX
COMMUNITY
Surveys
NEWS
Current
Press
Archive
FEATURES
Editorial
Dr. Mac
Reviews
Reader Reports
RESOURCES
FAQ
Documentation
Learning Center
MAN pages
Glossary
Tutorials
Tips
Links

OUR PARTNERS

OSXFAQ Mac OS X UNIX Tip-of-the-Day  back to index

Redirection - Combine Redirections

By Adrian Mayo - Editor - OSXFAQ

Because of the order in which Bash processes a command line and redirection syntax, you must be careful combining the merging of standard out and standard err (1>&2 or 2>&1) with redirection.

This example uses an 'ls' command line that lists 'file' (which fails) and the directory 'test' that contains 'file':

$ ls test file
ls: file: No such file or directory
test:
file

We merge standard out into standard error and redirect standard error (hopefully this is now both out and error) to the file 'err'.

Our first attempt fails:

$ ls test file 1>&2 2>err
test:
file
$

Reversing the order of the redirect and the merge will yield the desired behaviour:

$ ls test file 2>err 1>&2
$ cat err
ls: file: No such file or directory
test:
file
$

The reason the first attempt fails is that out is merged with error at the time error is still going to the screen. Only later is error redirected to the file, too late for out to be redirected too. (I've always though that the two syntaxes work the opposite way around to what seems natural.)

To throw away either or both of standard out and standard error, redirect to the special file /dev/null. For example, if we like only good news:

$ ls test file 2>/dev/null
test:
file
$

Visit the Site of the Book of the Unix Tips:
Project 6 covers redirection and pipelining in detail.


Discuss this trick in the OSXFAQ Learning Center forum

E-mail your comments or suggestions to webmaster@osxfaq.com


Copyright © 2000-2010 Inside Mac Media, Inc. All rights reserved.
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users.
Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc.