Bash

Making Stderr go to both Stderr and Stdout

Say you have a cron job where stout goes to syslog and stderr goes to email. You probably want the stderr to also go to syslog. Here is one highly convoluted way to do it:

exec 3>&1

ls foo bar 2>&1 >&3 | tee -a /dev/fd/3 >&2

exec 3>&-
  1. Takes stout and saves it's state to fd 3
  2. run ls with stderr pointed to stdout and stout pointed to fd 3 (which is connected to your screen)
  3. tee takes the stderr from ls and copies it to both /dev/fd/3 (your screen) and stdout
  4. the stdout of the tee command is redirected to sterr so the original stderr is restored
  5. fd 3 is closed

/!\ Lines of stderr will not be interleaved with lines of stdout. Instead stderr will probably be bunched at the bottom of the output.


Bash (last edited 2008-12-23 18:20:57 by dmartin)