Reputation: 1367
I am trying to check in the script, that if that script is executed with -x flag which is for the debugging for shell scripts. Is there any way to check that in script itself that -x is set. I want to conditionally check that and do something if that is set.
Upvotes: 5
Views: 1405
Reputation: 1511
Look for xtrace
in $SHELLOPTS
.
For example:
if grep -q xtrace <<<"$SHELLOPTS"; then
DO_SOMETHING;
fi
Upvotes: 0
Reputation: 1197
You can trap the DEBUG signal, like so:
trap "do_this_if_it_is_being_debugged" DEBUG
function do_this_if_it_is_being_debugged() {
...
}
Note this needs to be executed before the set -x
is being executed
Upvotes: 2
Reputation: 1641
Use:
if [[ $- == *x* ]]; then
echo "debug"
else
echo "not debug"
fi
From Bash manual:
($-, a hyphen.) Expands to the current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the -i option).
Upvotes: 10
Reputation: 72639
The portable way to do this (without bashisms like [[ ]]
) would be
case $- in
(*x*) echo "under set -x"
esac
Upvotes: 2