Sending Both Output and Error Messages to Different Files
Problem
You are expecting output from a program but you don’t want it to get littered with error messages. You’d like to save your error messages, but it’s harder to find them mixed among the expected output.
Solution
Redirect output and error messages to different files.
$ myprogram 1> messages.out 2> message.err
Or more commonly:
$ myprogram > messages.out 2> message.err
Discussion
This example shows two different output files that will be created by the shell. The first, messages.out, will get all the output from the hypothetical myprogram redirected into it. Any error messages from myprogram will be redirected into message.err.
In the constructs 1>
and
2>
the number is the file
descriptor, so 1 is STDOUT and 2 is STDERR. When no number is specified,
STDOUT is assumed.
Get bash Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.