Global variables, and their scope in Bash.
Cameron Hutchison
lists at xdna.net
Mon Dec 14 03:07:47 UTC 2009
Ray Parrish <crp at cmc.net> writes:
>I take it then that each of my functions is a "subshell", so the value
>of a global variable would not be alterable by any of them?
No. A function is not a subshell. It executes within the current shell
instance.
The right-hand sides of a pipeline are executed in a subshell though. I
noted earlier that you had a construct of:
some command | while read a b c ; do
some other command
...
done
Anything within that while loop is in a subshell (it's on the right-hand
side of the pipeline. Any changes you make to variables in that while
loop will not be propagated to the parent shell.
A subshell is a child process of the main process. Any changes in a
child process cannot be simply propagated up. You will need to either
restructure your code or use some method of IPC.
Googling for "bash variables in while loop pipeline" should give you
more info on this topic.
More information about the ubuntu-users
mailing list