As an example consider the following bash script. There are two loops, the first one executes in background and the second one prints myvar values:
#!/bin/bash
myvar=AAA
while true;
do
sleep 3
myvar=BBB
sleep 3
myvar=CCC
done &
while true;
do
echo "${myvar}"
sleep 1
done
The output I actually get:
AAA
AAA
AAA
...
The output I would like to get:
AAA
BBB
CCC
BBB
CCC
...
This is due to the
&creating a new subshell for the first while loop.I’m pretty certain you’ll need to use some kind of IPC to solve this. Using a pipe or a named pipe to implement a producer/consumer setup would be reasonable.
A rough example:
Outputs: