The parent process can't get variable from the child. You should to use some form of communication between processes to exchange data. You can use:
- file
- the child write the info into an file, like:
newvar=someval
- when the child exits, the parent
source the file and get the newvar variable
or
- named pipe
- the parent create a fifo
- runs the child in the background and start read from the pipe
- the child write the information to the pipe
- parent read it...
demo:
file: parent.sh
commfile="./.ex.$$"
echo "the newvar before child: =$newvar="
bash child.sh "$commfile"
source "$commfile"
echo "the newvar after source: =$newvar="
rm -f "$commfile"
file: child.sh
commfile="$1"
#do something
sleep 2
echo "newvar='some value'" >"$commfile"
running parent.sh prints:
the newvar before child: ==
the newvar after source: =some value=