Reputation: 14490
When trying to both pipe the output of a command into another, and also have the second command read from a herestring, I do not get the results I expected. For example:
echo "a" | grep -f - <<<abc
I would expect to produce the output
abc
but I get nothing instead
Upvotes: 1
Views: 32
Reputation: 14490
Both the pipe and the herestring are trying to become stdin
for the second command, here grep
. Using strace
on it shows that only the herestring, in this case, is actually available to grep
. It then gets an empty "file" to search, and finds no matches. Here is a portion of strace
when the search space is a file instead of a herestring:
read(0, "a\n", 4096) = 2
read(0, "", 4096) = 0
<snip>
read(3, "abc\n", 32768) = 4
read(3, "", 32768) = 0
but with the herestring we instead see:
read(0, "abc\n", 4096) = 4
read(0, "", 4096) = 0
<snip>
read(0, "", 32768) = 0
so we never read the values from the pipe that we expected to have been our pattern space.
Using a process substitution does get around this problem, because then either the pattern space or the search space are not coming from file handle 0:
echo "a" | grep -f - <(echo "abc")
for example produces the exected abc
output
Upvotes: 4