Here is an example:
bash-4.2$ export i=0
bash-4.2$ while :; do let i=i+1; done &
[1] 29668
bash-4.2$ echo $i
0
bash-4.2$ kill %1
bash-4.2$ exit
Here, the job I started keeps increasing $i but only as a local variable. I know in bash there is no such a thing as pointers or references, so my question is how to do it?
If it’s possible somehow, then it would mean it’s possible to access and write one variable in more than one threads at the same time, which is what I want to achieve. Is there a way to prevent memory reading/writing problems with something like critical sections (in shell/bash)?
Bash 4 supports coprocesses which is simply a behind-the-scenes method of using a two-way pipe.