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?
Best Answer
set -x
orset -o xtrace
expands variables and prints a little + sign before the line.set -v
orset -o verbose
does not expand the variables before printing.Use
set +x
andset +v
to 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
set
attributes, and on debugging.