I've never really thought about how the shell actually executes piped commands. I've always been told that the "stdout of one program gets piped into the stdin of another," as a way of thinking about pipes. So naturally, I thought that in the case of say,
A | B,
A would run first, then
B gets the stdout of
A, and uses the stdout of
A as its input.
But I've noticed that when people search for a particular process in
ps, they'd include
grep -v "grep" at the end of the command to make sure that
grep doesn't appear in the final output.
This means that in the command
ps aux | grep "bash" | grep -v "grep" it is implied that
ps knew that
grep was running and therefore is in the output of
ps. But if
ps finishes running before its output gets piped to
grep, how did it know that
grep was running?
flamingtoast@FTOAST-UBUNTU: ~$ ps | grep ".*" PID TTY TIME CMD 3773 pts/0 00:00:00 bash 3784 pts/0 00:00:00 ps 3785 pts/0 00:00:00 grep