Logging shell scripts' executions

Small hack. Do you have a bash1 script that runs via cron or incron or any other daemon, and you wanna debug it by at least tracing its execution? Well, simply surround it by:

(

set -x

...

) > /tmp/$0-$(date +%F.%T).log

But! Do not make the same mistake I did and forget to redirect stderr too2!:

) > /tmp/$0-$(date +%F.%T).log 2>&1

I know there is a shortcut for that, but I'm too lazy to look it up.


  1. I'm not sure this works in sh

  2. Othewise the trace does not appear in the log, because it's output to stderr