The following is tested on OpenBSD
A new shell is spawned to handle the pipeline and
while ... done loop:
Code:
#!/bin/sh
VAR=Old
echo A | while read REPLY; do
echo Here
VAR=New
echo "Inside while ... done loop: VAR = $VAR"
pgrep sh | wc -l
done
echo VAR = $VAR
pgrep sh | wc -l
Running this produces:
Code:
Here
Inside while ... done loop: VAR = New
10
VAR = Old
9
So there is one more shell running inside that loop. The variable is set to within that sub-shell and this sub-shell cannot modify the variable of the parent shell/process.
If you mess around with shell commands in Makefiles you encounter similar issues