Bash – Turn on xtrace with Environment Variable

bashenvironment-variablestrace

Bash has a sometimes-useful feature whereby if you turn on the "-x" option (I believe the symbolic name is xtrace), Bash outputs each line of script as it executes it.

I know of two ways to enable this behavior:

  • In the script itself, say set -x
  • On the command line, pass the -x option to Bash.

Is there any way of turning this option on via environment variables?

(In particular, I'm not invoking Bash myself, so I can't pass any options to it, and the script of interest is inside a compressed archive which I don't really feel like rebuilding. If I could set an environment variable, it would presumably be inherited by all child processes…)

  • The manpage says something about BASHOPTS, but when I try it Bash says that's read-only. (Thanks for not mentioning that in the manpage.)

  • Similarly, SHELLOPTS also seems to be read-only.

  • You can select which FD is used with BASH_XTRACEFD. But I still need to turn tracing on in the first place.

Best Answer

Use env to ignore the readonly flags.

env SHELLOPTS=xtrace ./yourscript
Related Question