This script:

mkfifo $tmppipe
echo "test" > $tmppipe
cat $tmppipe

Does not terminate. I assume that the cat command is waiting for an EOF from the pipe; how do I send one?


No, it's

echo test > "$tmppipe" # BTW, you've got the quotes in the wrong places

that hangs. More precisely, it's the shell opening the pipe for writing before running echo.

pipe are inter-process communication mechanisms, they are to be used between processes running concurrently. Here, the open(WR_ONLY) (>) will block until another process does an open in read mode.

echo test > "$tmppipe" &
cat < "$tmppipe"

will work because echo and cat run concurrently.

On Linux, you can get away with:

exec 3<> "$tmppipe"
echo test >&3
cat < "$tmppipe"
exec 3<&-

That works because read+write opens (<>) on pipes don't block on Linux, and because the test\n output by echo is small enough to fit in the pipe, so you can do the write and the read sequentially.

It wouldn't work for a larger output like:

exec 3<> "$tmppipe"
seq 100000 >&3
cat < "$tmppipe"
exec 3<&-

Because seq would fill up the pipe (64kiB in current versions of Linux) and block until some other process reads data off that pipe, which will never happen because cat won't run until seq has finished.

Note that:

echo test 1<> "$tmppipe"
cat < "$tmppipe"

would not work either because the echo command line would open the pipe, write test and then close the pipe (and then the system would destroy it as there's no file descriptor open to it anymore). So the next cat command line would try to instantiate a new pipe (and block until something opens the fifo file for writing).

  • Position of quotes really doesn't matter in this case, as $tmppipe contains no special characters. I just habitually put quotes around the argument to echo for readability - I'm used to other languages where string literals require quotes, so this way it is visually clear that "test" is a string being echoed. – Benubird Jun 5 '15 at 10:29
  • @Benubird, note that it's not only about special (wildcard) characters, it's also with any character contained in $IFS. Leaving a variable unquoted (unless you have a very good reason to) is bad practice leading to bugs and potential security issues. Quotes in shell have almost an opposite meaning as with other languages. – Stéphane Chazelas Jun 5 '15 at 10:52

Turns out the answer is obvious - the pipe is being locked by echo, and never reaches cat!

Pipes do not store data. When a process attempts to write to a pipe, the write cannot complete until there is something attached to the other end of the pipe, to read it.

A way to solve this particular example, is to use

echo "test" > $tmppipe &

to make the writing process run in the background. That way it sits there any waits, while the script continues, until it reaches the cat and can complete.

  • Strictly speaking, pipes store a buffer-full of data (the contenance of the pipe as in a real-life pipe), but pipes only live as long as there are file descriptors open to them. For fifo files, pipes are intentiated only when both a fd for reading and one for writing have been open to the file (or one fd in read+write on some systems), subsequent opens (of the same fifo file) while that pipe is active reuse the same pipe. Note that here, it's not writing to the pipe that blocks, but open it first (before running echo) – Stéphane Chazelas Jun 5 '15 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.