Monday, February 21, 2022

[SOLVED] linux: why if you pipe ps to grep, grep filter is in process output ? How does pipe works?

Issue

With a typical command like ps aux |grep foobar why grep foobar process is in output?

I was thinking thatps aux output was passed to grep to filter the lines by the pattern in grep argument but at the time you execute ps aux grep is still not executed (at least it's what I was supposing, I am wrong for sure), is executed later.

How does pipe works then?


Solution

The reason is simple:

  1. OS starts the grep process (as the pipe target).
  2. OS starts the ps, which finds grep running.
  3. OS connects the standard output of ps to the standard input of grep.


Answered By - Antonio Petricca
Answer Checked By - Gilberto Lyons (WPSolving Admin)