On Wed, Apr 30, 2014 at 02:04:02PM -0700, Viktor Szépe wrote: > I am surprised of these different values: > > echo "MAIN $BASHPID" ; echo "first $BASHPID" >&2 | echo "subshell $BASHPID" > MAIN 30551 > subshell 8514 > first 8513 > > Why neither side of the pipe has pid 30551? > Why are TWO new subshells needed?
Each command of a pipeline is run in a subshell. There is one exception: the LAST command in the pipeline MAY be run in the parent shell, if the lastpipe option is set. This option was added in bash 4.2 and does not work with job control enabled. AT&T Korn shell (ksh88 and ksh93) has always run the last pipeline command in the parent shell. However, pdksh does NOT do it (at least, last time I tried it).