Parent and Child Shell Script Problem Parent and Child Shell Script Problem shell shell

Parent and Child Shell Script Problem


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/bashexport g_var=2. ./child.sh

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


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).


Use below command in child file - This will export all the variable to temporary file env_fileprintenv | sed 's/^/export /;s/=/=\"/;s/$/\"/' > env_file

and then in parent file after calling the child file use below command

./child.sh. env_file