I've noticed that variable scope within a bash for loop seems to change if I pipe the output of the loop.
For example, here g
remains changed after the loop:
$ g=bing; for f in foo; do g=fing; echo g in loop: $g; done; echo g after $g;
g in loop: fing
g after fing
whereas here, the change during the loop is forgotten:
$ g=bing; for f in foo; do g=fing; echo g in loop: $g; done | cat; echo g after $g;
g in loop: fing
g after bing
The value of g
in the receiver of the pipe is from the "outer" context too:
$ g=bing; for f in foo; do g=fing; echo g in loop: $g; done | (cat; echo in pipe $g;); echo g after $g;
g in loop: fing
in pipe bing
g after bing
What's going on?
From the bash man page
This means that both sides of the pipeline are run in a subshell.
From http://www.tldp.org/LDP/abs/html/subshells.html
This means that when the pipeline ends all changes to variables are lost.
Here is a proof of concept for this theory using
BASH_SUBSHELL
Input:
Output:
As you can see both inside the loop and the second part of the pipe have been run inside subshells, and they end at the end of the pipeline.
Edit 2
Realised it was probably clearer to do this to show the different subshells that are run
Bash <4.0
In old bashes it doesn't include $BASHPID which is really the only way to see the pid of subshells, but you can declare a function like
which works pretty much the same
Bash 4.0+
Output:
As you can see this makes it clearer that before and after the pipeline you are in the same shell with the original variable.
Your third case is also solved as both sides of the pipe run in different subshells the variable is reset to the parent value for each piped command, so will have reverted back after the loop, even though it is still the same pipeline.
As soon as you use a pipe (
|
) subshells are involved, mostly on both sides of the pipe.Therefore the for loop runs in a subshell and sets the variable inside that subshell. That's why after the loop the variable value stayed.
In your first example there is no subshell, just multiple commands executed after each other.