Bash variable scope

Because you’re piping into the while loop, a sub-shell is created to run the while loop.

Now this child process has its own copy of the environment and can’t pass any
variables back to its parent (as in any unix process).

Therefore you’ll need to restructure so that you’re not piping into the loop.
Alternatively you could run in a function, for example, and echo the value you
want returned from the sub-process.

http://tldp.org/LDP/abs/html/subshells.html#SUBSHELL

Leave a Comment