Skip to content
The Bash Insectarium

/static/insetario/en/inseto/coproc/

Menu

BSH-016 · Processes

coproc

A background process with two pipes wired to the shell.

coproc launches a command in the background and leaves two descriptors on the table: ${NAME[0]} reads its output, ${NAME[1]} writes to its input. The PID sits in $NAME_PID.

It is a two-way conversation with a living process — a calculator, an interpreter — with no files and no named FIFOs. It arrived in bash 4.0; zsh has its own, older coproc, spoken to with print -p and read -p.

$ coproc CALC { bc -l; }
[1] 4242
$ echo "3*7" >&${CALC[1]}
$ read -u ${CALC[0]} resposta
$ echo $resposta
21
bc answers through the pipe, without leaving the background.