Can't write to named pipe

12,957

A named pipe remains opened until you read it from some other place. This is to permit communication between different processes.

Try:

mkfifo fifo
echo "foo" > fifo

Then open another terminal and type:

cat fifo

If you return to you first terminal, you'll notice that you can now enter other commands.

See also what happends with the reverse :

# terminal 1
cat fifo

# terminal 2
echo "foo" > fifo

# and now you can see "foo" on terminal 1

If you want you terminal not to "hang on" when trying to write something to the fifo, attach to the fifo a file descriptor :

mkfifo fifo
exec 3<> fifo
echo "foo" > fifo
echo "bar" > fifo
Share:
12,957

Related videos on Youtube

tay10r
Author by

tay10r

I enjoy writing software, writing music, and playing chess.

Updated on August 03, 2022

Comments

  • tay10r
    tay10r over 1 year

    I'm trying to write to a named pipe, made with mkfifo. But when I run the command, (ex) ls > myNamedPipe, I can no longer enter commands into the bash. I can still write characters and that's pretty much it.

  • Joost
    Joost over 10 years
    Thanks - this works great! Can you explain me why, though? I don't quite get how the exec 3<> fifo line fixes things..
  • chepner
    chepner about 10 years
    Attaching the fifo to a file descriptor causes the shell to buffer data written to the fifo. Without it, any write to the fifo blocks until something reads what is written. With the file descriptor, "foo" and "bar" are buffered by the shell, and anything reading from that file descriptor can read them later.