1. Home
  2. Computing & Technology
  3. Linux

From

more), but some tend to forget about redirecting errors - output which might be depended upon later on. Also, if you are lucky, errors will be mailed to you and eventual causes of failure might get revealed. If you are not as lucky, errors will cause your script to fail and won't be caught or sent anywhere, so that you can't start to do any worthwhile debugging.

When redirecting errors, note that the order of precedence is significant. For example, this command, issued in /var/spool


   

ls -l * 2 > /var/tmp/unaccessible-in-spool

will redirect output of the ls command to the file unaccessible-in-spool in /var/tmp . The command


   

ls -l * > /var/tmp/spoollist 2 >& 1

will direct both standard input and standard error to the file spoollist . The command


   

ls -l * 2 >& 1 > /var/tmp/spoollist

directs only the standard output to the destination file, because the standard error is copied to standard output before the standard output is redirected.

For convenience, errors are often redirected to /dev/null , if it is sure they will not be needed. Hundreds of examples can be found in the startup scripts for your system.

Bash allows for both standard output and standard error to be redirected to the file whose name is the result of the expansion of FILE with this construct:

&> FILE

This is the equivalent of > FILE 2>&1 , the construct used in the previous set of examples. It is also often combined with redirection to /dev/null , for instance when you just want a command to execute, no matter what output or errors it gives.

Explore Linux
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Linux
  4. Linux Documentation
  5. Bash Guide for Beginners
  6. Bash Guide For Beginners - 8.2.3. Redirection and file descriptors

©2009 About.com, a part of The New York Times Company.

All rights reserved.