At the cost of a process per occasion, you can use:
(set -x; ls $dir)
This runs the command in a sub-shell, so the set -x
only affects what's inside the parentheses. You don't need to code or see the set +x
. I use this when I need to do selective tracing.