Course contentsShow
← Linux
Lesson 46 of 1,8191. Shell Architecture and Process EnvironmentFree lesson

Co-processes and Bidirectional IPC

Manage bidirectional communication between the parent shell and background background co-processes.

In this lesson, you will learn how to maintain a persistent, two-way conversation between your main shell script and a background process using the coproc keyword.

Beyond Simple Pipes

So far, you’ve learned about standard pipelines (|) and named pipes (mkfifo), which are excellent for sending data in one direction—from A to B. However, sometimes you need a "long-lived" worker process where you send a request, get an answer, do some math, and send another request without closing the connection. This is bidirectional Inter-Process Communication (IPC).

The coproc Mechanism

In Bash, a co-process is a background process that is connected to the parent shell via two pipes: one for input (stdin) and one for output (stdout). When you prefix a command with coproc, the shell automatically creates these pipes and assigns them to file descriptors.

Here is how you initialize a simple co-process:

# Start a co-process that reads lines and echoes them back
coproc my_worker {
    while read line; do
        echo "Worker received: $line"
    done
}

# The shell populates these variables automatically:
# ${my_worker[0]} is the read end (for the shell to read from the worker)
# ${my_worker[1]} is the write end (for the shell to write to the worker)

echo "Hello Worker" >&${my_worker[1]}
read -u ${my_worker[0]} response
echo "Shell got: $response"

Why This Matters

Unlike a regular subshell or a pipeline that terminates once the data is processed, the co-process stays alive in the background, waiting for more input. By using >&${name[1]} (to write) and read -u ${name[0]} (to read), you maintain a persistent "chat" with that process.

This approach avoids the overhead of spawning a new process for every single interaction, making it significantly more efficient for tasks requiring repeated, stateful communication. Just remember that because the pipes are inherited, you should always close them when you are finished to avoid resource leaks in your shell environment.

Key Takeaway: The coproc command enables persistent, bidirectional communication by keeping a background process running and mapping its input/output streams to shell variables.