user1527227
user1527227

Reputation: 2238

pipe STDOUT of a command to several programs at once (bash)

According process substitution in bash, stdout of one command can be piped into several programs at once using the following template:

echo 'foobar' | tee >(command1) >(command2) | command3

So, you could do:

echo "the fox jumped over the lazy dog" | tee >(grep fox) >(grep jumped)

And get the output of all three commands.

Now I tried storing the output of all these commands, but with no success:

echo "the fox jumped over the lazy dog" | tee >(n1=$(grep fox)) >(n2=$(grep jumped))
echo $n1, $n2

You will see $n1 and $n2 are empty! Why? Is there a way to make this work?

Thank you.

Upvotes: 1

Views: 193

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80931

For the same reason that the following outputs bar:

$ foo=bar
$ $(foo=quux)
$ echo $foo
bar

Assignments in sub-shells (or in your case entirely separate processes) do not make changes in the parent (or entirely unrelated) shell.

Upvotes: 1

Related Questions