views:

79

answers:

2

Hi ,

I have two scripts parent.sh and child.sh. There is a variable in parent.sh which needs to be accessed by the child process. I have achieved this by exporting the variable in the parent script and the variable is available to the child process?

Is there any way that child can modify the value of the export variable defined in parent shell?

Parent.sh

#!/bin/bash
export g_var=2
./child.sh

child.sh

#!/bin/bash
g_var=`expr $g_var + 1 `   #This modification is available in child shell only.
A: 

No (assuming that you mean you need the change propogating back to the parent). You'll have to implement some form of IPC (inter-process communication) in whatever fashion to communicate this change.

(A simple way would be for the child to write out a small shell script containing the change, and for the parent to source this script when the child process exits).

Brian Agnew
Please provide me some more detail about this implementation
Sachin Chourasiya
+2  A: 

Most shells support the source built-in, which executes a script in the current shell rather than spawning a new one. In bash and a few others, the command . is equivalent to source. I haven't read it, but the POSIX shell standard might require the two to be equivalent. Try:

#!/bin/bash
export g_var=2
. ./child.sh

"child.sh" is the same as your example.

outis