Coprocess facility

The Korn shell, or POSIX shell, allows you to run one or more commands as background processes. These commands, run from within a shell script, are called coprocesses.

Designate a coprocess by placing the |& operator after a command. Both standard input and output of the command are piped to your script.

A coprocess must meet the following restrictions:
  • Include a newline character at the end of each message
  • Send each output message to standard output
  • Clear its standard output after each message
The following example demonstrates how input is passed to and returned from a coprocess:
echo "Initial process"
./FileB.sh |&
read -p a b c d
echo "Read from coprocess: $a $b $c $d"
print -p "Passed to the coprocess"
read -p a b c d
echo "Passed back from coprocess: $a $b $c $d"
FileB.sh
   echo "The coprocess is running"
   read a b c d
   echo $a $b $c $d
The resulting standard output is as follows:
Initial process
Read from coprocess: The coprocess is running
Passed back from coprocess: Passed to the coprocess

Use the print -p command to write to the coprocess. Use the read -p command to read from the coprocess.