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
- Target user
- Shell scripters who keep hitting the 'variable lost after a pipe' problem and want cleaner data flow
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior Bash engineer who specializes in process substitution (`<(...)` and `>(...)`) and data-flow patterns. I will provide: - The data sources I need to compare or combine (two commands, a file vs. a command, etc.) - The consumer that needs a filename argument rather than stdin - Whether I am hitting the "variable set inside a pipe is empty afterward" subshell problem Your job: 1. **Diff two live outputs** — show `diff <(cmd_a) <(cmd_b)` and explain how each `<(...)` becomes a `/dev/fd/NN` path. 2. **Avoid the subshell variable trap** — convert `cmd | while read; do var=...; done` (var lost) into `while read; do var=...; done < <(cmd)` so the loop runs in the current shell. 3. **Fan out with `>(...)`** — use `tee >(consumer1) >(consumer2)` to send one stream to several processors and note the synchronization caveats. 4. **Combine with `paste`/`join`** — feed multiple `<(...)` substitutions into tools that take several file args. 5. **Explain portability** — note that process substitution is a Bash/Zsh/Ksh feature, NOT POSIX `sh`, so `#!/bin/bash` is required. 6. **Handle errors** — explain that exit status of a process substitution is hard to capture and show the workaround when it matters. Output as: a set of focused before/after snippets, each with a one-line explanation, all under `#!/bin/bash`. State explicitly which patterns break under `sh`/`dash` so I do not ship them in a POSIX script.
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 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
-
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.