Using tee to create a log

Top Page
Attachments:
Message as email
+ (text/plain)
Delete this message
Reply to this message
Author: Wayne Conrad
Date:  
Subject: Using tee to create a log
On Thu, 26 July 2001, Alan Dayley wrote:
> At 11:56 AM 7/26/01 -0700, you wrote:
> >If foo writes to stderr as well as stdout, some lines won't get captured
> to the file (you'll see them scrolling by). If that happens and you want
> to capture those lines too, try this:
> > foo &>logfile
>
> This one did it. I had tried ">" but it did not capture anything. I did
> not know about the "&>" to capture stderr. Evidently this program spits
> everything to stderr.


Cool! I'm glad it worked.

Some follow-on noise, if you wanted to know more...

If you wanted to use tee with your program that writes to stderr, then you'd do this:

foo 2>&1 | tee logfile

This runs foo, redirecing foo's stderr to stdout so that it gets piped normally. Then we pipe stdout to tee; tee writes everything it reads to the file and writes it to standard-out.

Add "2>&1" to your bag of tricks, along with "&>". You'll use it often.

"&>" is really a shorthand for "2>&1 >". These two commands do the same thing:

  foo &> logfile            # bash-specific shorthand for...
  foo 2>&1 > logfile        # this, which works in both bash and ksh


I hope this isn't too much (or too confusing).

        Wayne