Simple Question: using export in sh shell

If at command line, in a sh shell I do:

Code:
VAR1=100
export VAR1
echo $VAR1

It will display 100.

If I put first two lines in a script file a.sh and the last line in a script file b.sh and then I execute:
Code:
./a.sh
./b.sh

It echoes nothing.
Any idea why?

I've tried before setting VAR1 to do a:
Code:
set -e

and at the end to do a:
Code:
set +e

But still I cannot use/pass VAR1 in second script.
 
Export puts the variable into the environment of the currently running shell, so that any new shells that are started (sub-shells) can read it.

In your example, the export line in a.sh puts the variable into the environment of that shell. When the script ends, the environment goes with it. Then b.sh is run, with a clean environment.

You need to either:
  • call b.sh from within a.sh, or
  • export the variable in your shell, then run a.sh and b.sh
 
In the end I've placed all "global" variables in filename.conf and then in each shell scripts at the begining after #!/bin/sh I've used:
Code:
. filename.conf
 
Back
Top