Why pipe resets current working directory?

bash, shell

Solution

Quoting from the manual:

Each command in a pipeline is executed in its own subshell (see Command Execution Environment).

Also see Grouping Commands:

`{}`

   { list; }

Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created.

Problem

First script: ``` $ { mkdir dir; cd dir; pwd; } | cat; pwd; ./dir . ``` Second script: ``` $ { mkdir dir; cd dir; pwd; }; pwd; ./dir ./dir ``` Why this `| cat` has an effect on current directory? And how to solve it? I need the first script to work exactly as the second one. I don't want `cat` to change my current directory back to `.`.

Original source

Related problems