Yevgeniy P
Yevgeniy P

Reputation: 1614

Bash process substitution with pipe

Suppose I have a file t.txt with many lines containing 'a'. I m puzzled why this doesn't work:

cat <(tail -f t.txt | grep a)

The above command just hangs without printing anything, even though every line has a match. Is this because cat is waiting for output of "tail" instead of "grep"? How can I fix this?

Btw, I tried another variant with double process substitution:

cat <(grep a <(tail -f t.txt))

This also hangs without printing anything.

Does anyone have a clue?

Upvotes: 1

Views: 356

Answers (2)

o11c
o11c

Reputation: 16146

For programs that don't take a --line-buffered argument, you can use stdbuf:

cat <(tail -f t.txt | stdbuf -oL grep a)

How this works is sheer magic; it's best not to think about it.

Upvotes: 2

Cyrus
Cyrus

Reputation: 88959

Add grep's option --line-buffered.

See man grep.

Upvotes: 6

Related Questions