views:

201

answers:

2

In a bash 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. Perhaps there is a better way of generating such a a log?

+6  A: 

set -o verbose, or set -v, set -x seems to be the way

http://www.faqs.org/docs/abs/HTML/options.html

$ cat shl
#!/bin/bash                                                                     

DIR=/tmp/so
ls $DIR

$ bash -x shl 
+ DIR=/tmp/so
+ ls /tmp/so
$
Tom
+2  A: 

set -x will give you what you want.

Here is an example shell script to demonstrate:

#!/bin/bash
set -x verbose #echo on

ls $PWD

This expands all variables and prints the full commands before output of the command.

output:

+ ls /home/user/
file1.txt file2.txt
radman
Using the word "verbose" that way doesn't accomplish anything. You can do `set -o verbose` or `set -v` (only "verbose") or `set -o xtrace` or `set -x` (only "xtrace") or `set -xv` (both) or `set -o xtrace -o verbose` (both).
Dennis Williamson