It is currently Tue, 07 Dec 2021 02:06:45 GMT

Author Message
 Redirecting output of one command to both stdout and stderr
Simple question; how can I redirect the output of a command to go to
both stdout and stderr?

For example, I have a script that runs periodically, and I am capturing
stdout and stderr into cumulative logs.  I want a date stamp recorded
in each of the logs for each time the script runs.  Right now I have
"date; date >&2" at the top of the script to accomplish this.  Works
fine, but I was hoping for something a little more refined than just
running the same command twice.


 Mon, 12 Nov 2007 00:32:49 GMT   
 Redirecting output of one command to both stdout and stderr
On 2005-05-25, Bub <> wrote:

For your example, there isn't any solution that is obviously better.
You could do

printf "%s\n"  "$OUTPUT"
printf "%s\n"  "$OUTPUT" >&2

If you have support for it, then you can do

date | tee /dev/stderr

(maybe replacing /dev/stderr with /dev/fd/2 or /proc/self/fd/2)

Dan Bernstein used to have a nice program called 'multitee', which is
still available (with source) for Debian at least, based on the BSD
program of the same name. It does exactly what you want.

date | multitee 0-1,2

 Mon, 12 Nov 2007 04:31:24 GMT   
   [ 2 post ] 

Similar Threads

1. Redirecting stdout and stderr outputs

2. Redirect stdout * stderr to one file in bourne sh using exec

3. Redirecting Stdout seperatly from Stderr with the at command

4. Redirecting stdout and stderr, and stderr

5. how to bash redirect stdout stderr without buffering

6. Redirect stdout and stderr....

7. redirecting stdout,stderr and stdin to /dev/null

8. Problem redirecting stderr+stdout in rsh script

9. forking, redirecting stdout & stderr

10. problem redirecting both stdout and stderr

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software