Bash Coprocess Bidirectional Pipe Orchestration Prompt
Drive a long-running interactive child process from a Bash script using a coproc so you can stream commands in and read responses out without re-spawning the process per call
- Target user
- Platform engineers wiring shell automation around stateful CLIs (database REPLs, gpg-agent, custom daemons)
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior automation engineer who specializes in Bash inter-process communication and the `coproc` keyword.
I will provide:
- The interactive command I need to keep alive (e.g. `psql`, `bc`, a custom REPL) and its prompt/output delimiter behavior
- The sequence of requests I want to send and how I expect to recognize each reply boundary
- My Bash version and target platform (so you can flag `coproc` portability limits)
Your job:
1. **Spawn the coprocess** — scaffold `coproc NAME { cmd; }` and explain the auto-created `NAME[0]` (read) and `NAME[1]` (write) file descriptors plus `NAME_PID`.
2. **Write requests safely** — show how to `printf '%s\n' "$req" >&"${NAME[1]}"` and why unbuffered/line-buffered output on the child matters.
3. **Read replies without hanging** — implement a bounded `read -r -u "${NAME[0]}"` loop with `read -t` timeouts and a sentinel/delimiter to detect end-of-reply.
4. **Guard against deadlock** — explain the classic pipe-buffer deadlock and how to avoid it (one coproc per shell limitation, fd leakage into subshells).
5. **Tear down cleanly** — close both fds with `exec {fd}>&-`, signal the child, and `wait "$NAME_PID"`, capturing its exit status.
6. **Handle the single-coproc constraint** — if I need more than one, show the manual `mkfifo` + background-process alternative.
Output as: one annotated `.sh` script using `set -euo pipefail`, with a clearly marked request/response loop and inline comments at each fd operation.
Call out explicitly any place where a missing line-buffer flush on the child will silently hang the read loop forever.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Bash Process Substitution Patterns Prompt
Use process substitution to feed command output where a filename is expected — diffing two live command outputs, tee-ing to multiple consumers, and avoiding subshell variable loss
-
Bash Multi-Repo Git Bulk Command Runner Prompt
Build a Bash script that runs the same git or shell command across many local repositories with per-repo isolation, dirty-tree guards, and an aggregated pass/fail summary
-
Bash Safe In-Place sed Edit Wrapper Prompt
Build a Bash wrapper around sed -i that backs up, validates, and atomically applies in-place edits so a bad expression can never corrupt or truncate config files
-
Bash Health-Gated Service Restart Orchestrator Prompt
Build a Bash script that restarts services one at a time, waits for each to pass a health check before moving on, and rolls back or aborts on failure to avoid taking a whole fleet down at once
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.