Operating System - HP-UX
1833034 Members
2339 Online
110049 Solutions
New Discussion

Re: How to return variable to parent shell?

 
SOLVED
Go to solution
Igor Sovin
Super Advisor

How to return variable to parent shell?

Hi!

My problem is the following
For example I do
#sh
#a=1
#echo $a
1
#exit
#echo $a

#

How to get value of variable "a" in my parent shell?

thanx
6 REPLIES 6
Biswajit Tripathy
Honored Contributor
Solution

Re: How to return variable to parent shell?

exit $a

- Biswajit
:-)
Igor Sovin
Super Advisor

Re: How to return variable to parent shell?

I tried that
it doesn't work
Biswajit Tripathy
Honored Contributor

Re: How to return variable to parent shell?

My last post will basically return value of $a to the
parent shell, so "echo $?" in parent shell after
execution would give the value of a (i.e 1 in your
example).

I'm not sure if that what you asked. If you want to
set the variable a in the parent shell, then that can't
be done. You can't set a parameter to some value
automatically in the parent shell. You can only set a
variable to a value in a child shell by using "export a".

- Biswajit
:-)
Igor Sovin
Super Advisor

Re: How to return variable to parent shell?

sorry, I've mistaken
everything works
thank you!
Biswajit Tripathy
Honored Contributor

Re: How to return variable to parent shell?

See the following thread on the topic:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=14148

- Biswajit
:-)
Bob_Vance
Esteemed Contributor

Re: How to return variable to parent shell?

The thread mentioned only discusses using a temp file to store the value. This is a tried and true and good way to do it. However, you then have the little temp file housekeeping to contend with -- delete it , etc.

For simple cases and depending on what you're doing, you don't need a file. Simply use Command Substitution.

In the following, in the local shell, we're setting xx to the standard output of the execution of a child shell. The child shell does some testing and then echoes (to standard out) various values depending on the results of his tests. The parent shell (our shell) can then check the value of xx:

Pine3 ## touch /tmp/foo ;\
xx=$(
if [ -f /tmp/foo ]
then echo 'foo_exists'
else echo 'foo_gone'
fi
);\
if [ "$xx" = 'foo_exists' ]
then echo "\n**it's all good!**"
else echo "\n**crap! where's foo?**"
fi

**it's all good!**


Pine3 ## rm /tmp/foo ;\
xx=$(
if [ -f /tmp/foo ]
then echo 'foo_exists'
else echo 'foo_gone'
fi
);\
if [ "$xx" = 'foo_exists' ]
then echo "\n**it's all good!**"
else echo "\n**crap! where's foo?**"
fi

**where's foo?**


HTH
bv
"The lyf so short, the craft so long to lerne." - Chaucer