1

Note that I have reviewed both stackoverflow questions below, but my question is different:

In attempting to do this, I have the following test.sh script:

#!/bin/bash

rm stdout_and_stderr.log
rm stderr.log

exec 2> >(tee -ia stderr.log >> stdout_and_stderr.log) 1> >(tee -ia stdout_and_stderr.log)

echo "stdout"
echo "stderr" >&2

The only problem is that stderr is not displayed to terminal:

$ ./test.sh > /dev/null
$ ./test.sh 2> /dev/null
$ stdout

My version of bash:

GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
Community
  • 1
  • 1
dabest1
  • 2,347
  • 6
  • 25
  • 25

1 Answers1

0

Here is a solution that works:

#!/bin/bash

rm stdout_and_stderr.log
rm stderr.log

exec 2> >(tee -ia stdout_and_stderr.log >&2)
exec 2> >(tee -ia stderr.log >&2)
exec 1> >(tee -ia stdout_and_stderr.log)

echo "stdout"
echo "stderr" >&2

It can also be done in one line:

exec 1> >(tee -ia stdout_and_stderr.log) 2> >(tee -ia stdout_and_stderr.log >&2) 2> >(tee -ia stderr.log >&2)
dabest1
  • 2,347
  • 6
  • 25
  • 25