Passing variables between scripts

Hi all.

I need to pass a value from a script that runs in a sub-shell, back into the calling shell like below (or into script 2 directly):

outer_script
export param=value1

script1 $param
(in script1: export param=value2)

script2 $param
($param is now value1, not value2 like i'd prefer)

end outer_script

I can't get this to work using regular variables and I have read some about shells and sub-shells and variables and I now believe that it's not possible with variables.

I guess I could always use a file to store the value in script1 and then read it in script2 using the file. But if I go this way I'd be forced to have separate files for each user that executes the script so that no conflicts occurr. But then I'd get a lot of files that mess up. I guess I could then remove each file after the script completes but what if the user terminates using Ctrl+C? I'd still have a lot of messy files after a while.

At the moment I'm leaning towards using files, but I thought I'd ask if someone knows another/better way to do this without using files?

This is in Korn Shell

In your script1, you have to return the value, beacause the export is lost at the end of script.

At the end of 'script1', do 'exit $param'

And try this :

outer_script
export param=value1

param=$(script1 $param)
(in script1: export param=value2)

script2 $param
($param is now value1, not value2 like i'd prefer)

end outer_script

Or try with mkfifo.

Open a queue for reading in the first script, then call second script and write to the queue from the second script. The first script will wait untill it has read something, then continue.

In order to have the value of a variable from a different script run within a another script, available globally, you need to source the inner script, instead of simply executing it.



outer_script

export param=value1

source /path/to/script1 
# or use the dot symbol :    .  /path/to/script1
(in script1: export param=value2)

script2 $param
# Now $param will be value2

end outer_script


# Now the new value (value2) of the variable param will be set globally.

Google source command for more info.

Thanks a lot, just what I needed!! :smiley: