Sandbox in a subshell:
(set -x; do_thing_you_want_traced)
Of course, changes to the variables or environment made in this subshell will be lost.
If you REALLY care about this, you can also use the DEBUG hook (using set -T to force it to be inherited by functions) to implement the native equivalent of set -x .
For example, if bash is used:
trap_fn() { [[ $DEBUG && $BASH_COMMAND != "unset DEBUG" ]] && \ printf "[%s:%s] %s\n" "$BASH_SOURCE" "$LINENO" "$BASH_COMMAND" return 0
However, emitting BASH_COMMAND (as the DEBUG trap can do) is not completely equivalent to set -x ; for example, it does not show values โโafter expansion.
source share