views:

1263

answers:

10

Can anyone explain this behavior? Running:

#!/bin/sh
echo "hello world" | read var1 var2
echo $var1
echo $var2

results in nothing being ouput, while:

#!/bin/sh
echo "hello world" > test.file
read var1 var2 < test.file
echo $var1
echo $var2

produces the expected output:

hello
world

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.

A: 

This also produces no output:

#!/bin/sh
echo "hello world" > test.file
cat test.file | read var1 var2
echo $var1
echo $var2

Not that it should matter, but what version of linux are you running?

num1
+2  A: 

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)
Mark Harrison
+3  A: 

Allright, I figured it out!

This is a hard bug to catch, but results from the way pipes are handled in Unix. Every element of a pipeline runs in a separate process. When the read command sets var1 and var2, is sets them it its own subshell, not the parent shell. So when the subshell exits, the values of var1 and var2 are lost. You can, however, try doing

var1 = $(echo "Hello")
echo var1

which returns the expected answer. Unfortunately this only works for single variables, you can't set many at a time. In order to set multiple variables at a time you must either read into one variable and chop it up into multiple variables or use something like this:

set -- $(echo "Hello World")
var1="$1" var2="$2"
echo $var1
echo $var2

While I admit it's not as elegant as using a pipe, it works. Of course you should keep in mind that read was meant to read from files into variables, so making it read from standard input should be a little harder.

num1
+3  A: 

This has already been answered correctly, but the solution has not been stated yet. Use ksh, not bash. Compare:

$ echo 'echo "hello world" | read var1 var2
echo $var1
echo $var2' | bash -s

To:

$ echo 'echo "hello world" | read var1 var2
echo $var1
echo $var2' | ksh -s
hello
world

ksh is a superior programming shell because of little niceties like this. (bash is the better interactive shell, in my opinion.)

Jon Ericson
+2  A: 

The post has been properly answered, but I would like to offer an alternative one liner that perhaps could be of some use.

For assigning space separated values from echo (or stdout for that matter) to shell variables, you could consider using shell arrays:

$ var=( $( echo 'hello world' ) )
$ echo ${var[0]}
hello
$ echo ${var[1]}
world

In this example var is an array and the contents can be accessed using the construct ${var[index]}, where index is the array index (starts with 0).

That way you can have as many parameters as you want assigned to the relevant array index.

Good solution. It works great in bash, but it doesn't work in the dash shell.
Ryan Ahearn
+1  A: 

read var1 var2 < <(echo "hello world")

Steve Baker
+2  A: 

Try:

echo "hello world" | (read var1 var2 ; echo $var1 ; echo $var2 )

The problem, as multiple people have stated, is that var1 and var2 are created in a subshell environment that is destroyed when that subshell exits. The above avoids destroying the subshell until the result has been echo'd. Another solution is:

result=`echo "hello world"`
read var1 var2 <<EOF
$result
EOF
echo $var1
echo $var2
pjz
+1  A: 
#!/bin/sh
echo "hello world" | read var1 var2
echo $var1
echo $var2

produces no output because pipelines run each of their components inside a subshell. Subshells inherit copies of the parent shell's variables, rather than sharing them. Try this:

#!/bin/sh
foo="contents of shell variable foo"
echo $foo
(
    echo $foo
    foo="foo contents modified"
    echo $foo
)
echo $foo

The parentheses define a region of code that gets run in a subshell, and $foo retains its original value after being modified inside them.

Now try this:

#!/bin/sh
foo="contents of shell variable foo"
echo $foo
{
    echo $foo
    foo="foo contents modified"
    echo $foo
}
echo $foo

The braces are purely for grouping, no subshell is created, and the $foo modified inside the braces is the same $foo modified outside them.

Now try this:

#!/bin/sh
echo "hello world" | {
    read var1 var2
    echo $var1
    echo $var2
}
echo $var1
echo $var2

Inside the braces, the read builtin creates $var1 and $var2 properly and you can see that they get echoed. Outside the braces, they don't exist any more. All the code within the braces has been run in a subshell because it's one component of a pipeline.

You can put arbitrary amounts of code between braces, so you can use this piping-into-a-block construction whenever you need to run a block of shell script that parses the output of something else.

flabdablet
+1  A: 

My take on this issue (using Bash):

read var1 var2 <<< "hello world"

echo $var1 $var2

A: 

Word = "Hello World"

echo $Word | while read i do char1 = $(echo $i | cut -d ' ' -f1 | tr -d ' ') char2 = $(echo $i | cut -d ' ' -f2 | tr -d ' ') done

echo "$char1" echo "$char2"