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

This is a pretty n00b question as I am still learning how the bash shell works. However, google did not yield any results that I could get. Let us say I have .c.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/bin/sh. I feedFeeding input to this executable as followslike 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 I feed in inputexecuting like this:

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

I observed thatkeeps the subshell invoked by ./foo persists./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./foo terminating ./foo./foo? But that does not make sense to me as the sys call should be a blocking one leading to ./foo./foo not terminating. I would appreciate help and a pointer to resources to fix erroneous understanding. Thanks!

This is a pretty n00b question as I am still learning how the bash shell works. However, google did not yield any results that I could get. 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. I feed input to this executable as follows:

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 I feed in input like this:

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

I observed that the subshell invoked by ./foo persists. 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!

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!

Source Link
iart
  • 121
  • 2

Understanding behavior of subshell and stdout with pipe

This is a pretty n00b question as I am still learning how the bash shell works. However, google did not yield any results that I could get. 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. I feed input to this executable as follows:

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 I feed in input like this:

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

I observed that the subshell invoked by ./foo persists. 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!