Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: pipe affects scope of globals?



On Mar 8,  1:18pm, Dwyer, William K wrote:
} 
} calling a function and piping it to read a variable seems to affect the
} scope of its internal variables!  huh?

Of course it does.  Pipes connect two unix processes; anytime you use a
pipe the shell creates at least one new process via the fork() system
call.   Unix processes do not share memory space [*], but all processes
created with fork() start out with a *copy* of the same memory space.
Hence two different "scopes".

It happens that most shells always put the right side of a pipe in a new
process created with fork(), but zsh puts the *left* side in the new
process and (if possible) keeps the right side in the current shell.
So in your example, "read" was able to change $y, which would not work
in (say) bash.

[*] Yes, sometimes you can cause two processes to share memory by doing
extra work, but the shell doesn't do that.  The shell's primary job is to
start new processes, not to try to guess whether they might want to peek
at each other's memory.



Messages sorted by: Reverse Date, Date, Thread, Author