bash recursive xtrace bash recursive xtrace bash bash

bash recursive xtrace


It is possible to make a subshell run using the same shell options set in the parent by exporting the SHELLOPTS environment variable.

In your case where X.sh and Y.sh cannot be edited, I'd create a wrapper script that simply exports SHELLOPTS before calling X.sh.

Example:

#!/bin/sh# example X.sh which calls Y.sh./Y.sh

.

#!/bin/sh# example Y.sh which needs to be called using sh -euxecho $SHELLOPTS

.

#!/bin/sh -eux# wrapper.sh which sets the options for all sub shellsexport SHELLOPTS./X.sh

Calling X.sh directly shows that -eux options are not set in Y.sh

[lsc@aphek]$ ./X.sh braceexpand:hashall:interactive-comments:posix

Calling it via wrapper.sh shows that the options have propagated to the subshells.

[lsc@aphek]$ ./wrapper.sh + export SHELLOPTS+ ./x.sh+ ./y.sh+ echo braceexpand:errexit:hashall:interactive-comments:nounset:posix:xtracebraceexpand:errexit:hashall:interactive-comments:nounset:posix:xtrace

Tested on GNU bash, version 3.00.15(1)-release. YMMV.


So i've got tool to debug shell scripts:

  #!/bin/sh  # this tool allows to get full xtrace of any shell script execution  # only argument is script name  out=out.$1  err=err.$1  tmp=tmp.$1  echo "export SHELLOPTS; sh $@ 1>> $out 2>> $err" > $tmp  sh -eux $tmp &> /dev/null  rm $tmp