I have a following very simple script which watches /tmp directory for new files and output of inotifywait is piped to sed:


/usr/local/bin/inotifywait -q -m /tmp --format %f | sed 's/a/b/'

If I check the output of pstree then I can see that both inotifywait and sed are indeed running:

  |                          `-sed(8210)

Now if I pipe the output of inotifywait to while loop instead of directly to sed:


/usr/local/bin/inotifywait -q -m /tmp --format %f | 
  while IFS= read -r file; do
    sed 's/a/b/' "$file"

..then according to pstree:

    |                          `-test(8981)

..and ps:

$ ps -p 8981 -o command
/bin/bash ./test

..it is the shell which is started and not the external utility sed. Am I correct that this is simply because while is the bash shell builtin?

  • What happens when inotifywait outputs something, so that the read completes and sed starts? – muru Nov 17 '15 at 13:29

The relation of processes in a pipe mainly depends on the shell you use.

Modern shells make all simple processes (commands) in a pipe direct children of the main shell.

Older shells do this in a different implementation specific way.

Some shells run a while loop inside a sub-shell when the input is redirected, others do not.

while is not a shell builtin command but part of the shell syntax.

Conclusion: Do not try to analyse parent child relations in complex shell commands unless you are the author of that shell and like to check whether things work the way they are currently intended in this specific shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.