Technical skill

Bash syntax: Coprocesses

Keep one helper ready for repeated questions

At a shop counter, a clerk may ask the same stockroom worker several questions while that worker stays in place. A computer program can do something similar: a main program handling file rows can send each row to one helper that checks it and returns an answer.

A process is a program that is currently running. A shell is a program that reads commands and starts other programs. A Bash script is a text file containing commands for Bash to run. Bash's coproc feature starts a helper as a separate process and leaves a way to send it input and read its output. Bash connects the two programs with pipes, which are one-way channels. A command's standard input is where it receives data; standard output is where it sends ordinary results.

A main worker sends requests to a helper through one channel and receives replies through a separate channel.

One channel carries a request to the helper; the other brings the reply back.

Start a simple helper

This named coprocess runs cat, a command that copies its input to its output:

coproc ECHOER { cat; }

Bash starts cat as the helper and keeps it running while the script talks to it. The helper is named ECHOER. The code below starts a fresh helper and sends it one line, then reads and prints the reply. It repeats the setup line so the example can run by itself:

coproc ECHOER { cat; }
printf '%s\n' 'maintenance complete' >&"${ECHOER[1]}"
IFS= read -r answer <&"${ECHOER[0]}"
printf '%s\n' "$answer"

The first printf sends the words maintenance complete to the helper. Since cat copies input to output, Bash can read the same line back into answer, and the last printf displays it. In a useful program, a helper could check or transform a request instead of echoing it.

How Bash names the channels

Bash stores the coprocess's two channel numbers in an array (a short list) named ECHOER. ECHOER[1] is the channel the shell writes to; it feeds the helper's standard input. ECHOER[0] is the channel the shell reads from; it carries the helper's standard output. These numbers are file descriptors, labels Bash uses for open input and output channels. The child process ID is available as ECHOER_PID. If the coprocess has no name, Bash uses the default array COPROC and PID variable COPROC_PID.

In the example, >& sends text to the helper's input channel and <& makes read use its output channel. IFS= and -r tell read to preserve whitespace and backslashes in the line. For repeated exchanges, both sides need to agree on where one reply ends. Sending one line and receiving one line is a simple protocol. Send diagnostic messages to standard error, or redirect them separately, so they do not get mistaken for replies.

Wait for completion and handle limits

A helper that reads until it sees end-of-file waits while the script still has its input channel open. Close that write channel after the final request so the helper knows no more input is coming. coproc starts the helper asynchronously and returns success without waiting for its eventual result. The wait command (here, wait "$ECHOER_PID") waits for the helper to finish and returns its exit status.

The programs run at the same time. Reading can pause until the helper sends an answer; writing can pause if the helper is not reading. Some programs buffer output, so a response may not arrive until they flush it. Bash runs the coprocess command in a subshell (a second shell process); keep channel use in the current shell instead of assuming another subshell inherits its array or open channels. For a one-way batch, a pipeline such as producer | consumer is usually simpler: the first program sends data directly to the second, without reading separate replies. POSIX is a standard for common shell behavior; coproc is a Bash feature, not part of every POSIX shell, so use Bash to run a script that depends on it. Check the helper's documentation for the request and reply format.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful