8

I know that to debug script I can issue command

set -x 

on the first line. The problem is that when script launches some other scripts they do not inherit this setting. So my question is whether there is some possibility to set this flag globally for shell and all subshells or for some script and all scripts launched by it?

4
  • Is there a reason to have a subshell? Commented Oct 14, 2013 at 13:00
  • 2
    +1 There's certainly nothing you can do to prevent an inner script from turning the option off explicitly, but aside from calling each script with bash -$- I don't see another way. Curiously awaiting answers. Commented Oct 14, 2013 at 15:18
  • @kojiro you should never need to edit source files to debug as it is, in itself, a source of bugs. The answer is passing the debug flags to bash on the command line and to use SHELLOPTS. See: stackoverflow.com/a/56136968/37370 Commented Mar 9, 2021 at 7:27
  • I don't think I said what you seem to think I said, lo, these eight years ago. Commented Mar 10, 2021 at 12:59

3 Answers 3

9

In Bash you can use export SHELLOPTS. It will make all Bash subshells inherit the -x option (as well as all the other options in SHELLOPTS!).

Example:

export SHELLOPTS
bash -x script1.sh

See bash recursive xtrace

Sign up to request clarification or add additional context in comments.

Comments

0

It depends, you call call your subshells with -x too,

2 Comments

I do not want to modify every call of script because main script can call some subscript and subscripts also call some scripts so modifying every call is unrealistic.
@Trismegistos, don't worry. it's quite obvious what you want and why.
-1

Put the set -x inside the Shell script

$ cat shell1.sh
echo "Shell1"

$ cat shell2.sh
#!/bin/bash
set -x
./shell1.sh
echo "shell2.sh"

$ ./shell2.sh
+ ./shell1.sh
Shell1
+ echo shell2.sh
shell2.sh

2 Comments

As you can see every command from shell2.sh is output to the screen but commands from shell1.sh are not output to the screen because -x flag is not inherited by shell1.sh script. What I want to achieve is that shell1.sh also output commands to the screen. I want to achieve that without modifying shell1.sh script. Bare in mind that shell1.sh in practive would also call some other scripts and I want them to inherit -x flag also.
ah that's tricky without modifying the shell1.sh. Lets wait for others to post their answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.