Unix shell scripts: pass shell parameters (-x, etc.) to nested scripts

How can I run nested shell scripts with the same option? For example,

parent.sh

#!/bin/sh
./child.sh

child.sh

#!/bin/sh
ls

How can I change parent.shit so that when I run it with a sh -x parent.shparameter -x, it is effective both in child.shand on my console, execution is displayed ls?

I am looking for a portable solution that is effective for rare situations, such as users of the system with /bin/falsea registered shell. Will there be any help to the variable $SHELL?

Clarification: I sometimes want to call parent.shwith -x, sometimes with -e, depending on the situation. Therefore, the solution should not include hard flag coding.

+4
2

bash, :

#!/bin/bash
export SHELLOPTS
./child.sh

, , echo $SHELLOPTS script , , , .

/bin/sh /bin/sh: SHELLOPTS: readonly variable - , POSIX /bin/sh , : https://lists.gnu.org/archive/html/bug-bash/2011-10/msg00052.html

+1

, .
,

, - sh:

alias saveShell='cp /bin/sh $some_safe_place'

alias shx='cp $some_safe_place /bin/x_sh; rm /bin/sh; echo "/bin/x_sh -x $@" > /bin/sh; chmod 755 /bin/sh '

alias she='cp $some_safe_place /bin/e_sh; rm /bin/sh; echo "/bin/e_sh -e $@" > /bin/sh; chmod 755 /bin/sh '
alias restoreShell='cp  $some_safe_place /bin/sh'

:
saveShell, shx she, -x -e restoreShell, shx she script, sh./parent.sh

SH

#!/bin/sh #!/bin/sh -x #!/bin/sh -e sed sh script.

+1

Source: https://habr.com/ru/post/1627404/


All Articles