In a shell script, how do I echo all shell commands called and expand any variable names?
For example, given the following line:
ls $DIRNAME
I would like the script to run the command and display the following
ls /full/path/to/some/dir
The purpose is to save a log of all shell commands called and their arguments. Is there perhaps a better way of generating such a log?
set -xorset -o xtraceexpands variables and prints a little + sign before the line.set -vorset -o verbosedoes not expand the variables before printing.Use
set +xandset +vto turn off the above settings.On the first line of the script, one can put
#!/bin/sh -x(or-v) to have the same effect asset -x(or-v) later in the script.The above also works with
/bin/sh.See the bash-hackers’ wiki on
setattributes, and on debugging.