Question
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
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?
How-To
set -x or set -o xtrace expands variables and prints a little + sign before the line.
set -v or set -o verbose does not expand the variables before printing.
Use set +x and set +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 as set -x (or -v) later in the script. Below is a sample bash script to show the effect:
- test.sh
- #!/bin/sh
- set -x
- echo "Repeat command"
- set +x
- echo "No repeat command"
Supplement
* set - Linux Command - Unix Command
沒有留言:
張貼留言