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 th
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.