Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

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”?

like image 890
dspjm Avatar asked Jul 25 '13 08:07

dspjm


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

Does subshell inherit environment?

Just as important are the things that a subshell does not inherit from its parent: Shell variables, except environment variables and those defined in the environment file (usually .

What does set +E mean in bash?

set -e The set -e option instructs bash to immediately exit if any command [1] has a non-zero exit status. You wouldn't want to set this for your command-line shell, but in a script it's massively helpful.

Which command make the variables available to subshells?

To make a variable available in a subshell (or any other subprogram of the shell), we have to “export” the variable with the export command.


1 Answers

export SHELLOPTS 

for example:

echo date > b chmod +x b 

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

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

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

$ echo "export SHELLOPTS; ./b" > a $ bash -xv a  ./a + ./b  date ++ date    Sun Dec 29 21:34:36 EST 2013 
like image 182
rgiar Avatar answered Sep 27 '22 21:09

rgiar