A colleague recently introduced me to bash -x for debugging my bash scripts without taking the echo $foo route.

$ bash -x hello-world.sh 
+ echo 'Hello World'
Hello World

Internally -- how does bash accomplish this? I checked man bash and it (didn't seem to?) mention the -x flag when invoking the interpreter. I'm mainly looking for an overview of what will and won't be debugged in the -x enviornment, or any other gotchas.


-x is the same as setting xtrace with the -o option to bash. From my copy of the bash manpage:

After expanding each simple command, for command, case command, select command, or arithmetic for command, display the expanded value of PS4, followed by the command and its expanded arguments or associated word list.

I don't think there are "gotchas" with this: it prints each command it executes.

(You can also conditionally enable this inside scripts or in interactive shell sessions with set -x to turn it on and set +x to turn it off again.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.