$ cat anotherscript
$ x=69; ./anotherscript; echo $x
"Can't I do this with export?" Nope, export only exports downwards in the process tree.
When you want to modularize your shell scripts, by wrapping pieces in little modules, how can you do it?
One way is to write the result to standard out:
$ cat anotherscriptThis works, but if you want to pass up more than one variable, it gets awkward.
$ x=69; x=$(./anotherscript); echo $x
A second is to source the module.
$ cat anotherscriptThis runs everything in the same shell, so it exposes everything the module's doing. That's not exactly "modular."
$ source anotherscript
$ echo $x, $y
Here's a third approach that lets you "export" variables "upwards." You have to use eval, but it gives you what you want with a reasonable syntax.
$ cat example
source raise.sh # a magic module
raise foo bar # mark variables to be "raised"
foo=hello # do stuff with them in the script
$ eval $(example) # now use the script
$ echo $foo, $bar # and see that they're available.
hello, world$ cat raise.sh
trap "4eval $*" EXIT
for i in $*