I would like to run two bash scripts simultaneously executed by a parent script that exports a variable with read and write privileges between all these scripts. Let's say I have 3 scripts: parent.sh
, children1.sh
, children2.sh
. From parent.sh
I am exporting a SOME_VAR
variable and executing ./children1.sh & children2sh
simultaneously. Then in children1.sh
and children2.sh
I am performing some operation. In addition, we can see children2.sh
is overriding SOME_VAR
variable with value 20
. I would like to have this change to be reflected in parent.sh
. So, for example, let's say I have such scripts:
parent.sh
#!/bin/bash
SOME_VAR=100
export SOME_VAR
bash ./children1.sh & ./children2.sh
echo "New SOME_VAR value: $SOME_VAR"
children1.sh
#!/bin/bash
for i in {1..3}
do
echo "### FIRST PROCESS $i"
done
children2.sh
#!/bin/bash
for i in {1..3}
do
echo "### SECOND PROCESS $i"
done
SOME_VAR=20
Now if I execute bash ./parent.sh
I get such output:
### SECOND PROCESS 1
### FIRST PROCESS 1
### SECOND PROCESS 2
### FIRST PROCESS 2
### SECOND PROCESS 3
### FIRST PROCESS 3
New SOME_VAR value: 100
Unfortunately, it looks like that children2.sh
operation was not reflected back to parent.sh
and SOME_VAR
variable is still equal to 100
instead of 20
. I came up with the idea that each script can create a file and write its output to this file. Later, these files can be read by the parent script after the children's scripts finish their work. This solution sounds a bit hacky to me. I am not a bash
expert, so maybe someone can help me with a more sufficient solution.
There is one requirement: You can NOT use any 3rd party libraries/tools like for example, GNU Parallel