Can't write to named pipe

tay10r picture tay10r · Mar 13, 2013 · Viewed 11.8k times · Source

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.

Answer

michaelmeyer picture michaelmeyer · Mar 13, 2013

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