Two-Way Communication With Coprocesses
Keep a program running and exchange data with it over pipes.
Use coproc NAME { cmd; } to get read and write file descriptors for the process.
01Command
Command
$ coproc BC { bc; }; echo "2+2" >&"${BC[1]}"; read -r answer <&"${BC[0]}"; echo "$answer"02What it does
A coprocess runs a command in the background with a pipe in each direction, so a script can send input and read output from a stateful program like bc or a database CLI without restarting it. This interactive automation is only possible from a shell.
03Risk
!Warning: The file descriptor numbers live in the COPID array and can be clobbered by other redirections; capture them immediately.
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- bash.1.en.html manual pageEvidence: The bash manual page documents coprocesses.
Last verified: 2026-10-13 · Category: Developer