Coprocess communication preview
coproc NAME { commands; } starts a Bash-specific asynchronous worker with two pipe ends exposed to the parent. NAME[0] reads worker stdout; NAME[1] writes worker stdin; NAME_PID identifies the worker process. With an unnamed coprocess the array is COPROC. The launch status is not the final work status: save the PID immediately and use wait. The shell can unset the special array and close its handles after the worker finishes, so coordinate the lifetime; a saved descriptor number alone does not guarantee it is open. The example uses a request, reply and acknowledgement to keep the worker alive until the parent reads the response. Both sides use bounded reads, and the parent waits for completion. Do not leave a worker waiting forever for input or read until EOF while keeping its input open. Do not assume the special descriptors are inherited by arbitrary children. For one-way data flow, a normal pipeline is often simpler. Full concurrency, cleanup and signals are covered later; this preview is for one trusted, short-lived worker, not a production IPC protocol. The two handshake examples use small fixed messages. Read timeouts do not impose a universal deadline on writes or the whole protocol. Check each write; if the request, reply or acknowledgement operation fails, exit the disposable parent script rather than announce success. The worker has bounded reads for this fixture.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Bounded request and reply
coproc WORKER { IFS= read -r -t 2 request || exit 2 printf 'reply:%s\n' "$request" || exit 5 IFS= read -r -t 2 ack || exit 3 [[ $ack == done ]]}pid=$WORKER_PIDout=${WORKER[0]}in=${WORKER[1]}printf '%s\n' 'daily report' >&"$in" || exit 5IFS= read -r -t 2 reply <&"$out" || exit 4printf '%s\n' "$reply" || exit 5printf 'done\n' >&"$in" || exit 5wait "$pid"printf 'worker status=%s\n' "$?"Expected stdout:
reply:daily reportworker status=0The acknowledgement ensures the worker stays alive until the reply is read. The saved PID is used to wait.
Observe a worker failure
coproc WORKER { IFS= read -r -t 2 request || exit 2 printf 'reply:%s\n' "$request" || exit 5 IFS= read -r -t 2 ack || exit 3 exit 7}pid=$WORKER_PIDout=${WORKER[0]}in=${WORKER[1]}printf '%s\n' 'daily report' >&"$in" || exit 5IFS= read -r -t 2 reply <&"$out" || exit 4printf '%s\n' "$reply" || exit 5printf 'done\n' >&"$in" || exit 5wait "$pid"printf 'worker status=%s\n' "$?"Expected stdout:
reply:daily reportworker status=7The initial coproc launch succeeds; wait reports the worker exit status 7.
Prefer a pipeline for one-way work
printf '%s\n' 'daily report' | { IFS= read -r request; printf 'reply:%s\n' "$request"; }Expected stdout:
reply:daily reportNo persistent worker or response handshake is needed for this one-shot transformation.
Common mistakes
Never use sleep as a synchronization guarantee. Use a defined handshake and timeouts; do not assume a fast-exiting worker still has accessible descriptors.
Practice
Run the request/reply fixture in a fresh Bash script. Change only the fixed request text, predict the reply, and explain which pipe each descriptor represents. Compare with the ordinary pipeline.
Next: Files, directories and text utilities