Skip to main content
1 of 4
Kusalananda
  • 355.7k
  • 42
  • 735
  • 1.1k

coproc utility is not the same as utility & in bash.

With coproc utility you get an array, COPROC, that contains the standard input and output filedescriptors of utility. You may then do things like

#!/bin/bash

coproc bc -l

for (( k = 0; k < 50; ++k )); do
  printf "2.3*%d + 1\n" "$k" >&${COPROC[1]}
  read -u "${COPROC[0]}" a
  printf '%.2f\n' "$a"
done

kill "$COPROC_PID"

Here, bc -l is a co-process and acts like a "arithmetic computation service" for the shell loop, taking expressions to compute on its standard input and giving back results on its standard output.

Kusalananda
  • 355.7k
  • 42
  • 735
  • 1.1k