Shell exec and pipes

bash, command-line, exec, pipe, shell

Solution

There is sentence in bash manual:

Each command in a pipeline is executed as a separate process (i.e., in a subshell).

So in both examples two processes are spawned by the pipeline first and 'exec' is executed inside one of spawned process - without impact on shell executing the pipeline.

Problem

I'm using bash, and as I understand it, exec followed by a command is supposed to replace the shell without creating a new process. For example, ``` exec echo hello ``` has the appearance of printing "hello" and then immediately exiting, because after echo is done, the shell process isn't there to return to anymore. If I put this as part of a pipeline - for instance, ``` exec echo hello | sed 's/hell/heck/' ``` or ``` echo hello | exec sed 's/hell/heck/' ``` my expectation is that, similarly, the shell would terminate as a result of its process being replaced away. This is not what happens in reality, though - both the commands above print "hecko" and return to the shell normally, just as if the word "exec" wasn't there. Why is this?

Original source