Tracing shell scripts is pretty easy, you just do sh -vx script.sh
, but tracing startup scripts like ~/.profile
and ~/.shrc
take a bit more work. Here's some ready-made stuff:
If you want to trace ~/.profile
, you add these lines to the beginning and end of that file:
# tron
if [ "$DO_TRACE" = 1 ]
then set -vx
exec 8>&1 9>&2 >/tmp/sh.$$.log.txt 2>&1
fi
[...original contents of ~/.profile...]
# troff
if [ "$DO_TRACE" = 1 ]
then exec 1>&8 2>&9 8>&- 9>&-
set +vx
fi
then activate tracing by setting the variable DO_TRACE
to 1
before the shell starts:
$ env DO_TRACE=1 xterm -ls
The trace output is left in /tmp/sh.NNN.log.txt
, where NNN
is the pid
of the shell.
And, it might be more convenient to put the tracing code into separate files and source
them in each file that needs it instead of duplicating the stuff everywhere:
. $HOME/lib/tron
[...original script...]
. $HOME/lib/troff
Caveat: the script that you're tracing should not be using the file descriptors (8
and 9
) that we use for tracing. If it does, you'll have to change those to something else.