Bash Scripting – Bash tail -f with while-read and Pipe Hangs

bashpipetail

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?

Best Answer

tail -n 3 -f /tmp/file0.txt | grep --line-buffered '.*' | while read LINE0 
do 
    echo "${LINE0}"; 
done 
Related Question