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
Forums
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 - Redirect and Pipe Both

By Adrian Mayo - Editor - OSXFAQ

Monday's tip showed how we can make our scripts write error messages to standard error instead of standard out, just like 'real' Unix commands. Today, we'll look at how to negate this convention by merging the standard error from a command back into its standard out.

For example, suppose we wish to send both the standard out and standard error of 'ls' to the same file. We use the special redirection '&>'. First, we'll test 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
$

Next, we'll redirect standard out to 'out':

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

Finally, we'll redirect both to the same file using the special redirection '&>':

$ ls test file &> out
$

Normally, when we pipe the output of one command to another, only standard out is piped. If you need to pipe both standard out and standard error, first merge standard error into standard out, then pipe standard out as normal. Here's an example using the same 'ls' command as above. We pipe to the 'awk' command, which then prints its input lines with "Piped: " added to the beginning.

First, we pipe only standard out:

$ ls test file | awk '{print "Piped: " $0}'
ls: file: No such file or directory
Piped: test:
Piped: file
$

Next, we pipe both standard out and standard error.

$ ls test file 2>&1 | awk '{print "Piped: " $0}'
Piped: ls: file: No such file or directory
Piped: test:
Piped: 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-2008 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.