Bash – Timing of ps piped to grep

bashgreppipe

So, this isn't a problem as such, just something I'm curious about. I'm using Linux Mint MATE which is branched off Debian. If I do:

ps afx  | grep abcdefg

I get:

16599 pts/3    S+     0:00  |   \_ grep --color=auto abcdefg

So, it's showing the process for the grep. But, that's after the ps in the pipe: I would have thought that the above does the ps, gets the results, and then passes them to grep. So how come the grep is actually showing up in the ps results? Doesn't it happen afterwards? I think I'm missing something fundamental about what the pipe actually does.

Best Answer

This question is a duplicate and belongs to unix.stackexchange.com.

To sum up, still, the OpenGroup's Shell Command Language doc is relatively vague on details regarding "pipelines":

A pipeline is a sequence of one or more commands separated by the control operator '|'. The standard output of all but the last command shall be connected to the standard input of the next command.

The format for a pipeline is:

[!] command1 [ | command2 ...]

The standard output of command1 shall be connected to the standard input of command2. The standard input, standard output, or both of a command shall be considered to be assigned by the pipeline before any redirection specified by redirection operators that are part of the command (see Redirection).

If the pipeline is not in the background (see Asynchronous Lists), the shell shall wait for the last command specified in the pipeline to complete, and may also wait for all commands to complete.

Note that while data clearly flows from "left to right" in the pipeline, there's no guarantee about the scheduling itself.

See also: