Can anyone explain this behavior? Running:
#!/bin/sh
echo \"hello world\" | read var1 var2
echo $var1
echo $var2
results in nothing being o
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)