
Exporting Enviroment Variables in shell scripts
Possibility number 1: a subshell (well, any subprocess) can pass data to
its parent by printing it to standard output.
Look at the shell function that ships with mc (Midnight Commander)?
When you run 'mc -P', mc prints out the cwd when you exit it. So, if you
want to use mc to change the working directory of your shell, you could
just define a function:
mc()
{
cd "`mc -P`"
}
That's it!
Well, to be truthful, the above shell function, although simple and
concise, will behave very badly if you would happen to suspend mc (hit
ctrl-z). To work around the problem, you should save the output of 'mc
-P' into a file. Midnight Commander ships with this sample shell
function that you can use.
mc ()
{
mkdir -p $HOME/.mc/tmp 2> /dev/null
chmod 700 $HOME/.mc/tmp
MC=$HOME/.mc/tmp/mc-$$
/usr/bin/mc -P "$@" > "$MC"
cd "`cat $MC`"
/bin/rm -f "$MC"
unset MC;
}
The script also takes care of passing other command line arguments
correctly to mc (the use of "$@") and allows you to run multiple mc's at
the same time (the temp file name includes the process id ($$)).
Usually you do not have to take excessive precautions like this and it
is enough to just run the subprocess inside backticks.