views:

25

answers:

2

How to pass values from child shell to parent shell in shell programming?

+1  A: 

This is impossible. The best that can be done is for the child to print the desired assignments, and for the parent to exec them.

Ignacio Vazquez-Abrams
can't we do that using global variables?
Sunil
A: 

See my answers here and here for discussions and demonstrations of variable scoping in the shell. There aren't any global variables.

The usual methods for passing values include printing the value or using a temporary file.

Example child and parent:

#!/bin/sh
# child.sh
a=4321
echo $a

and

#!/bin/sh
# parent.sh
val=$(child.sh)
echo $val
Dennis Williamson