Interview Prep/Shell Scripting/Arguments, I/O, here-docs, process substitution and subshells

What is `coproc` and when would you use it?

AdvancedConceptArguments, I/O, here-docs, process substitution and subshells
Answer

coproc starts a background process with two-way pipes: you write to its stdin and read its stdout via file descriptors in an array.

coproc BC { bc -l; }
echo "scale=4; 22/7" >&"${BC[1]}"
read -r result <&"${BC[0]}"
echo "$result"           # 3.1428

It is rare in DevOps scripts; it suits long-lived helpers (a bc calculator, a persistent psql session) where starting a new process per query is too slow. If you need this often, the task has probably outgrown Bash.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Arguments, I/O, here-docs, process substitution and subshells