bash: Coprocesses

1 
1 3.2.5 Coprocesses
1 -----------------
1 
1 A 'coprocess' is a shell command preceded by the 'coproc' reserved word.
1 A coprocess is executed asynchronously in a subshell, as if the command
1 had been terminated with the '&' control operator, with a two-way pipe
1 established between the executing shell and the coprocess.
1 
1    The format for a coprocess is:
1      coproc [NAME] COMMAND [REDIRECTIONS]
1 
1 This creates a coprocess named NAME.  If NAME is not supplied, the
1 default name is COPROC.  NAME must not be supplied if COMMAND is a
1 simple command (⇒Simple Commands); otherwise, it is interpreted
1 as the first word of the simple command.
1 
1    When the coprocess is executed, the shell creates an array variable
1 (⇒Arrays) named 'NAME' in the context of the executing shell.
1 The standard output of COMMAND is connected via a pipe to a file
1 descriptor in the executing shell, and that file descriptor is assigned
1 to 'NAME'[0].  The standard input of COMMAND is connected via a pipe to
1 a file descriptor in the executing shell, and that file descriptor is
1 assigned to 'NAME'[1].  This pipe is established before any redirections
1 specified by the command (⇒Redirections).  The file descriptors
1 can be utilized as arguments to shell commands and redirections using
1 standard word expansions.  The file descriptors are not available in
1 subshells.
1 
1    The process ID of the shell spawned to execute the coprocess is
1 available as the value of the variable 'NAME'_PID. The 'wait' builtin
1 command may be used to wait for the coprocess to terminate.
1 
1    Since the coprocess is created as an asynchronous command, the
1 'coproc' command always returns success.  The return status of a
1 coprocess is the exit status of COMMAND.
1