I am currently facing a problem with the command
while sleep 0.3; do echo "1"; done | tail -n +3 | grep --line-buffered "1"
I wanted an output that looks like:
[nothing during 0.6s]
1
1
...
but had:
[nothing forever]
I found this question and read this blog post but as the --line-buffered
option is set for grep
, I think this is not a buffering problem. My researches did not led me to other solutions, I hope I did not miss something obvious.
I am using zsh 5.7.1 (x86_64-pc-linux-gnu)
if it is important.
One of the most powerful shell operators is the pipe ( | ). The pipe takes output from one command and uses it as input for another. And, you're not limited to a single piped command—you can stack them as many times as you like, or until you run out of output or file descriptors.
You can make it do so by using the pipe character '|'. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command's output may act as input to the next command and so on.
The tail command, as the name implies, print the last N number of data of the given input. By default it prints the last 10 lines of the specified files. If more than one file name is provided then data from each file is precedes by its file name.
Tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then terminates. Example 1: By default “tail” prints the last 10 lines of a file, then exits. as you can see, this prints the last 10 lines of /var/log/messages.
As Barmar mentioned in the comments, this is getting swallowed up in a 4KB stdio buffer. You can use unbuffer -p
to avoid it:
while sleep 0.3; do echo "1"; done | unbuffer -p tail -n +3 | grep 1
Or you could also use stdbuf
in line-buffering mode:
while sleep 0.3; do echo "1"; done | stdbuf -oL tail -n +3 | grep 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With