Skip to main content
2 of 2
formatting; removed fluff
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k

Understanding behavior of subshell and stdout with pipe

Let us say I have .c file and corresponding executable (let us say foo) that takes in some input from stdin and then makes a sys call to /bin/sh. Feeding input to this executable like following:

python -c "<some script to feed input>" | ./foo 

I observed that the shell invoked by foo closes immediately and foo terminates. However, someone on IRC recommended that executing like this:

(python -c "<some script to feed input>"; cat) | ./foo 

keeps the subshell invoked by ./foo still running. I would like to know what is going on here.

My Speculation: For the first case, Does the stdout of the python script close as soon as it's done thus sending EOF to the stdin of the process ./foo terminating ./foo? But that does not make sense to me as the sys call should be a blocking one leading to ./foo not terminating. I would appreciate help and a pointer to resources to fix erroneous understanding. Thanks!

iart
  • 121
  • 2