Anchor(faq85)

How to write several times to a fifo without having to reopen it?

The basic use of named pipes is:

cat < myfifo &
echo 'a' > myfifo

this works but, cat dies after it. What if we want to write several times to the pipe without having to reopen it?

If the commands are consecutive, they can be grouped inside a subshell and redirect it's output:

cat < myfifo &
(echo 'a'; echo 'b'; echo 'c';) > myfifo

But if they can't be grouped for some reason, a better way is to assign a file descriptor to the pipe and write there:

cat < myfifo &

# assigning fd 3 to the pipe
exec 3>myfifo

# writing to fd 3 instead of the pipe
echo 'a' >&3
echo 'b' >&3
echo 'c' >&3

# closing the fd
exec 3>&-