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
2010-06-22 07:39:59
can't we do that using global variables?
Sunil
2010-06-22 08:13:19
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
2010-06-22 11:13:46