How to make bash scripts print out every command before it executes?
Solution 1
bash -x script
or
set -x
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; ...;)
Solution 2
These also work:
set -v
or
#!/bin/bash -v
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.
Solution 3
This should also work:
#!/bin/bash -x
cd ~/hello
ls
Solution 4
This should work:
set -o verbose #echo on
...
set +o verbose #echo off
Solution 5
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).
See more tips here
Related videos on Youtube
phantomCoder
Updated on September 17, 2022Comments
-
phantomCoder over 1 year
For example, I have a simple bash file
#!/bin/bash cd ~/hello ls
How can I make it display every command before executing it? Just the opposite effect of "@echo off" in windows batch scripting.
-
mkobit over 8 yearsFor those looking for what the
set
options are, you can find them on this man page. -
sobi3ch about 8 yearsYou can simply extend shebang like this
#!/bin/bash -x
-
-
Scott Pack almost 15 yearsI have always used this to great effect. The output looks a little dirtier than you might first expect.
-
Ape-inago almost 15 yearsGreat explanation! without doing any research, it looks to me like Verbose and eXplicit are what those two letters stand for.
-
Steven Parkes over 11 yearsI think -x is closer to eXecute.
-
Walter Tross over 9 yearsBy setting
PS4
you can tweak the prompt of-x
. E.g.:PS4='Line $LINENO @ $(date +%s.%N): ' bash -x script
(date
will slow it down, though) -
Eugene Pankov almost 9 years
-x
has nothing to with echoing in Python