Echo some command lines in a shell script (echo on for single command) Echo some command lines in a shell script (echo on for single command) shell shell

Echo some command lines in a shell script (echo on for single command)


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.


How about using this function?

runtraced() {    echo "$@"    "$@"}dosomethingruntraced dosomethingelse


Based on Jonathan Leffler answer, this works the same way, just a little more clear because there is noting needed after the command. But you need to specify which shell should be used. This is a example for sh:

sh -xc ls $dir