<p>Can anyone explain this behavior?
Running:</p>

<pre><code>#!/bin/sh
echo "hello world" | read var1 var2
echo $var1
echo $var2
</code></pre>

<p>results in nothing being ouput, while:</p>

<pre><code>#!/bin/sh
echo "hello world" &gt; test.file
read var1 var2 &lt; test.file
echo $var1
echo $var2
</code></pre>

<p>produces the expected output:</p>

<pre><code>hello
world
</code></pre>

<p>Shouldn't the pipe do in one step what the redirection to test.file did in the second example?  I tried the same code with both the dash and bash shells and got the same behavior from both of them.</p>

It's because the pipe version is creating a subshell, which reads the variable into its local space which then is destroyed when the subshell exits.
Execute this command
```
```
$ echo $$;cat | read a
10637
```
```
and use pstree -p to look at the running processes, you will see an extra shell hanging off of your main shell.
```
```
|                       |-bash(10637)-+-bash(10786)
    |                       |             `-cat(10785)
```
```