Coprocesses
Introduced with Bash 4.0, the coproc keyword allow allows the user to run a process in the background in an asyncronous subshell. During the of the process, a pipe is established between the calling shell and the coprocess. The best results are obtained with programs which can be run in a CLI and can read from stdin
and write to stdout
, better if with an unbuffered stream. The syntax for coprocess is here:
coproc (NAME) command (redirections)
The bits within parentheses are optional, but if you specify a name, coproc
will create a coprocess with the name. If no name is given, it will be defaulted to COPROC
; and we must not define any name if the following is a simple command, otherwise it will be treated as the first word of the command. The process ID of the shell executing the coprocess is stored in a variable calledNAME_PID
:
Let's see an example: zarrelli:~$ coproc { while true ; do ls ; done } [2] 31067
We executed an infinite loop and its PID
is shown as 31067
; let's check...