Operating System - HP-UX
1834879 Members
2049 Online
110071 Solutions
New Discussion

Re: export Variable problem

 
Vasudevan MV
Frequent Advisor

export Variable problem

Hi,

How do I export a variable from child to parent shell?.
Example:
A.sh
-----
export XYZ=10
B.sh
echo $XYZ

B.sh
------
export XYZ=30

but I can NOT run the B.sh in the same shell using this way like ". ./B.sh" in the A.sh script.

Thanks
Vasu

4 REPLIES 4
Stefan Farrelly
Honored Contributor

Re: export Variable problem

I believe the only way to do this is export the variable to a file and then in the parent sell check for the file and load the variable.

A better way to do it is declare the variable first in the parent, then when it gets modified in the child it will also be set back in the parent.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Thierry Poels_1
Honored Contributor

Re: export Variable problem

Hi,

simple answer: you cannot. The environment of a child process is lost when it exits.

You will need to "source" the script if you want to keep the environment. Why would you not use this option? :
. /dir/B.sh

Other tricks are to put the value in a temp file and read it, put the export command in a temp file and source it, ....

good luck,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Justo Exposito
Esteemed Contributor

Re: export Variable problem

Hi Vasu,

If you need only one value to be returned to the parent shell, you can do it by the exit from the child script like that:

A.sh
-----
export XYZ=10
sh B.sh
XYZ=$?
export XYZ
echo $XYZ

B.sh
------
export XYZ=30
exit $XYZ

Regards,

Justo.
Help is a Beatiful word
Peter Kloetgen
Esteemed Contributor

Re: export Variable problem

Hi Vasudevan,

*No way*, you can't do this, exporting works only in one direction, from parent processes to child processes, not the other direction. You have to put the value into a file which you read in parent process later to make the value existent here.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping