3

How can I redirect the output of a command such that both stdout and stderr gets logged in a file, AND I still want stderr to appear as output.

I also don't want to use bash for doing this. Is there such a way?

gablin
  • 4,678
  • 6
  • 33
  • 47
  • Usually shells do redirection. So when you say you don't want to use bash, what shell DO YOU want to use. – dcaswell Aug 25 '13 at 14:01
  • For bash see [Redirect stdout and stderr to file and stderr to stdout](http://stackoverflow.com/q/24001720/905686). – user905686 Dec 21 '15 at 17:08

1 Answers1

3

That is very easy:

$ ( ./command.sh >> log ) 2>&1 | tee -a log

You write the stdout of command to the log file in a sub shell; than you write stderr to pipe; and than, my means of tee, save it to the log and copy the same to the console.

Example of usage:

$ cat command.sh               
#!/bin/sh

echo out
echo err > /dev/stderr

$ ( ./command.sh >> log ) 2>&1 | tee -a log
err
$ cat log
out
err
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144