13

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.

MathematicalOrchid
  • 5,664
  • 7
  • 35
  • 62
  • Can you simply set `set -x` in the terminal and run the script? – Arkadiusz Drabczyk Aug 19 '19 at 11:49
  • Something like: `set -x; ./script.sh ; set +x` – Arkadiusz Drabczyk Aug 19 '19 at 11:52
  • 4
    @ArkadiuszDrabczyk The trace setting is not inherited by child processes. If it (and other shell settings) was, it would make writing scripts _really_ tricky, as you would have to either reset options in every script, or write alternative code paths for each eventuality. – Kusalananda Aug 19 '19 at 11:56
  • @Kusalananda: The example I posted works for me and if I understand correctly `./script.sh` is a child process, right? – Arkadiusz Drabczyk Aug 19 '19 at 12:00
  • 2
    @ArkadiuszDrabczyk In `bash` 5.0.7, it would trace the call to the shell script, but tracing would _not_ be turned on inside the script itself, i.e. the shell option would not be inherited by the script. – Kusalananda Aug 19 '19 at 12:01
  • @Kusalananda: ok, I got. Thanks for explanation. – Arkadiusz Drabczyk Aug 19 '19 at 12:04
  • If the script is inside a compressed archive, how are you running the script? – Kusalananda Aug 19 '19 at 12:10
  • @Kusalananda It's actually a script that gets executed when an RPM is installed. I'd rather not have to rebuild the RPM to debug why it isn't working. – MathematicalOrchid Aug 19 '19 at 12:15
  • Um... You would _have_ to rebuild the RPM once you've fixed it, so you might as well unpack it and and fix it properly from the start. You are likely to want to iterate over this process until the issue is solved. This is assuming that you are the maintainer of this RPM package. If you aren't, you could file a bug report to the maintainer. – Kusalananda Aug 19 '19 at 12:17
  • @Kusalananda If we run the script directly, it works perfectly. But if we install the RPM, something goes wrong somewhere. – MathematicalOrchid Aug 19 '19 at 12:37
  • So make a test RPM with the script and use `set -x` in it for testing. Also, you may want to ask a new question about rolling that RPM (with exactly what goes wrong and how it goes wrong). This seems to be your actual issue. – Kusalananda Aug 19 '19 at 12:39

1 Answers1

13

Use env to ignore the readonly flags.

env SHELLOPTS=xtrace ./yourscript
Charles Duffy
  • 1,651
  • 15
  • 22