Search code examples
linuxbash

How to let bash subshell to inherit parent's set options?


Let’s say I do set -x in a script “a.sh”, and it invokes another script “b.sh”.

Is it possible to let “b.sh” inherit the -x option from “a.sh”?


Solution

  • export SHELLOPTS
    

    for example:

    echo date > b.sh
    chmod +x b.sh
    

    without the export, we only see the commands in ./a.sh when it calls ./b.sh:

    $ echo ./b.sh > a.sh
    $ bash -xv a.sh
    
    ./a.sh
    + ./b.sh
    Sun Dec 29 21:34:14 EST 2013
    

    but if we export SHELLOPTS, we see the commands in ./a.sh and ./b.sh

    $ echo "export SHELLOPTS; ./b.sh" > a.sh
    $ bash -xv a.sh
    
    ./a.sh
    + ./b.sh  date
    ++ date   
    Sun Dec 29 21:34:36 EST 2013