0

I have two programs, one of them prints something first, they need to communicate with each other with their standard IO.

Is there a way in Bash to pipe stdout of two programs to each other? I know I could do this in C with forks and dups, but is there a way with pure Bash?

2

Create a named FIFO that will "loop" the pipe:

mkfifo myfifo
<myfifo program1 | program2 >myfifo
rm myfifo

Even if none of the programs prints anything first, you can "inject" data into the FIFO, so it's received by program1:

# from another console
echo start >myfifo

To observe/store the communication, use tee in the right place(s). Examples:

<myfifo program1 | program2 | tee myfifo
<myfifo program1 | tee /dev/tty ./log1 | program2 | tee ./log2 >myfifo

The whole thing will work in sh, there's nothing specific to Bash in this solution.

Please read this answer about possible scenarios leading to a deadlock.

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.