In Bash, piping tail -f
to a read
loop blocks indefinitely.
while read LINE0
do
echo "${LINE0}";
done < <( tail -n 3 -f /tmp/file0.txt | grep '.*' )
# hangs
Remove the -f
or | grep '.*'
, then the loop will iterate.
The following does not hang.
tail -n 3 -f /tmp/file0.txt | grep '.*'
What causes this behavior?
Is there anyway in Bash to follow a file and read in a pipe expression?
-f
, it is supposed to "hang" in the sense thattail
keeps waiting for more lines to be added to the file. When I trytail -n 3 -f /tmp/file0.txt | wc
, it hangs for me. It should hang becausewc
never gets an end-of-file signal. Are you sure that it doesn't hang for you?wc
withgrep
.grep
may not "hang" but it does have buffering issues. It may not print anything until there is enough to fill a buffer.tail -f
, but in theread
. If I grep for an expression that is only in a couple of lines, even if the file is grown large, the loop will not iterate. I now need to figure out how to stopread
from buffering.