Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash coproc (coprocesses)

Reference for the Bash coproc keyword: start a background command with a read and a write pipe, talk to it through COPROC file descriptors, and clean up safely.

A coprocess is a command that runs asynchronously in the background, like command &, but with two pipes attached: one connected to its standard input and one to its standard output. Bash keeps the other ends of those pipes open in your shell, so you can write requests to the process and read replies from it while the script keeps running. The keyword coproc (Bash 4.0 and newer) sets this up.

It fits tools that answer one line per request, such as bc, sed -u or a long-lived helper script, where you want to ask many questions without starting a new process each time.

Terminal
coproc [NAME] command [redirections]
coproc [NAME] { commands; }

Try it

Bash Talking to a coprocess

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash The default COPROC name and a one-shot job

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
coproc command args...              # simple command: the array is always named COPROC
coproc NAME { compound; commands; } # compound command: NAME is allowed
Name Description
NAME Optional name for a coprocess that runs a compound command ({ ...; }, ( ... ), a loop). Defaults to COPROC. A simple command cannot be named: Bash would read the name as the command.
NAME[0] File descriptor connected to the coprocess's standard output. You read from it.
NAME[1] File descriptor connected to the coprocess's standard input. You write to it.
NAME_PID Process ID of the coprocess (COPROC_PID for the default name).

Returns: the status of coproc itself is 0 once the process has been started. The coprocess's own exit status is available through wait "$NAME_PID".

Errors and edge cases: when the coprocess exits, Bash closes the descriptors and unsets NAME and NAME_PID, so data you did not read yet can be lost. Collect output before it finishes, or wait on the PID first.

Usage notes

  • Buffering is the usual trap. Most programs buffer output when it goes to a pipe, so a reply may not arrive until the buffer fills. Use line-buffered modes (sed -u, grep --line-buffered, stdbuf -oL) or write the helper in Bash, where echo flushes every time.
  • Closing a pipe end. Use exec {NAME[1]}>&- to close the write side and tell the process there is no more input. A plain exec "${NAME[1]}">&- does not work because the number is not a literal.
  • Only one at a time. Bash warns if you start a second coprocess while the first is still alive, because the descriptor handling is not designed for several. For parallel work, use named pipes (mkfifo) or background jobs.
  • Descriptor inheritance. Other background commands you start later also inherit the coprocess's pipe ends and may keep them open, so the coprocess never sees end-of-file.
  • -u with read. read -u "${NAME[0]}" avoids the <& redirection and is equivalent.
  • Alternatives. For a simple one-way stream use process substitution; for a whole second program that needs a terminal, use expect or a pipe pair created with mkfifo.
  • Not POSIX. coproc exists in Bash 4+ and Zsh uses different syntax (coproc with print -p). Dash and BusyBox ash do not have it.