Reputation: 2238
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
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