How to flush a pipe using bash

User1 picture User1 · Jul 28, 2010 · Viewed 12.5k times · Source

I have a script that writes to a named pipe and another that reads from the pipe. Occasionally, when starting the script I have noticed that the contents of the pipe exist from a previous run of the script. Is there a way to flush out the pipe at the beginning of the script?

Answer

mvds picture mvds · Jul 28, 2010

I think dd is your friend:

dd if=myfifo iflag=nonblock of=/dev/null

strace shows

open("myfifo", O_RDONLY|O_NONBLOCK)

and indeed doesn't even block on an empty fifo.