Search code examples
linuxbashshtee

How to redirect bash stderr to terminal, but also redirect stderr with stdout to log file?


I have a script that QA Manager runs. I want him to take a look at terminal from time to time to take a note if any errors appear.

The problem is that the script have a ton of output that we do not need to see.

What I am trying to implement:

  • stderr only goes to terminal
  • stdout only goes to full-log.txt
  • stderr goes to full-log.txt as well as stdout

Another way is to

  • stderr with stdout go to full-log.txt
  • stderr only go error-log.txt

Will be very grateful if someone can help with that.


Solution

  • with bash, using a process substitution, with the redirections performed in this order:

    cmd 2> >(tee -a full-log.txt) >full-log.txt
    

    Demo:

    $ { echo "this is stdout"; echo "this is stderr" >&2; } 2> >(tee -a full-log.txt) >full-log.txt
    this is stderr
    $ cat full-log.txt
    this is stdout
    this is stderr