When I do this:
find . -name "pattern" | grep "another-pattern"
Are the processes, find
and grep
, spawned together? My guess is yes. If so, then how does this work?:
yes | command_that_prompts_for_confirmations
If yes
is continuously sending 'y' to stdout and command_that_prompts_for_confirmations
reads 'y' whenever it's reading its stdin, how does yes
know when to terminate? Because if I run yes
alone without piping its output to some other command, it never ends.
But if piping commands don't spawn all the processes simultaneously, then how does yes
know how many 'y's to output? It's catch-22 for me here. Can someone explain me how this piping works in *NIX?
From the wikipedia page: "By itself, the yes command outputs 'y' or whatever is specified as an argument, followed by a newline, until stopped by the user or otherwise killed; when piped into a command, it will continue until the pipe breaks (i.e., the program completes its execution)."
yes
does not "know" when to terminate. However, at some point outputting "y" to stdout will cause an error because the other process has finished, that will cause a broken pipe, and yes will terminate.
The sequence is:
Yes, (generally speaking) all of the processes in a pipeline are spawned together. With regard to yes
and similar situations, a signal is passed back up the pipeline to indicate that it is no longer accepting input. Specifically: SIGPIPE
, details here and here. Lots more fun infomation on *nix pipelining is available on wikipedia.
You can see the SIGPIPE
in action if you interrupt a command that doesn't expect it, as you get a Broken Pipe
error. I can't seem to find an example that does it off the top of my head on my Ubuntu setup though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With