Export doesnt work for a back ground process

2019-07-25 09:14发布

问题:

pipe.sh

 export START=100
 . ./other.sh &
 wait

other.sh

sleep 5
export END=200

but I dont see the variable END in "export -p". But I do see it if change the pipe.sh to

 export START=100
 . ./other.sh 

how do I export variables from background process? Any work arounds?

回答1:

A child process cannot change parents environment, you need to declare the variable from the parent somehow. For example using a file:

pipe.sh:

export START=100
. ./other.sh > tmp &
wait
source tmp
rm tmp 
echo $END

other.sh:

sleep 5
echo "export END=200"

Also see this answer.



标签: bash shell