But -v doesn't print the PS4 string before each script line and it doesn't trace the steps of a "for" statement (for example) individually. It does echo comments while -x doesn't.
Here's an example of the output using -v:
#!/bin/bash -v
# this is a comment
for i in {1..4}
do
echo -n $i
done
1234echo
echo hello
hello
Here's the result of the same script with -x:
+ for i in '{1..4}'
+ echo -n 1
1+ for i in '{1..4}'
+ echo -n 2
2+ for i in '{1..4}'
+ echo -n 3
3+ for i in '{1..4}'
+ echo -n 4
4+ echo
+ echo hello
hello
Note that I included "echo -n" to add emphasis to the differences between -v and -x. Also, -v is the same as "-o verbose", but the latter seems not to work as part of a shebang.
set -o xtrace and set +o xtrace are your friends (it is more verbose, than -o verbose, and the output goes to STDERR, as opposed to verbose, which seems to log to STDOUT).
Including set -x in the script itself will enable the functionality while set +x will disable it.
Both of these methods will also work with the more portable sh shell.
If I remember correctly perl also has the -x option.
or
in the script.
You can unset the option again with
set +x
. If you just want to do it for a few commands you can use a subshell: `(set -x; command1; command; ...;)These also work:
or
But -v doesn't print the PS4 string before each script line and it doesn't trace the steps of a "for" statement (for example) individually. It does echo comments while -x doesn't.
Here's an example of the output using -v:
Here's the result of the same script with -x:
Note that I included "echo -n" to add emphasis to the differences between -v and -x. Also, -v is the same as "-o verbose", but the latter seems not to work as part of a shebang.
This should also work:
This should work:
set -o xtrace
andset +o xtrace
are your friends (it is more verbose, than-o verbose
, and the output goes to STDERR, as opposed to verbose, which seems to log to STDOUT).See more tips here
There are a few ways.
as the shebang line.
Including
set -x
in the script itself will enable the functionality whileset +x
will disable it. Both of these methods will also work with the more portable sh shell.If I remember correctly perl also has the -x option.
goes like
language = python, perl, bash -x = operator script = filename
hope it helps.
#! /bin/bash -x
does what you want.