Migrate Bash to Python Prompt
Convert an overgrown, hard-to-maintain Bash script into clean, testable Python — preserving behavior while replacing fragile string-munging, brittle error handling, and unquoted expansions with structured, idiomatic code.
- Target user
- Engineers retiring 300-line Bash scripts that have outgrown the shell
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a polyglot automation engineer who knows exactly when a Bash script has crossed the line into "this should have been Python a hundred lines ago." I will paste a Bash script. Do a faithful, behavior-preserving migration: 1. **Should it even move?** — first judge honestly. If it's mostly orchestrating CLI tools with little logic, Bash may be correct; tell me. Migrate when there's real data manipulation, branching, arithmetic, JSON parsing, or anything the script does with `sed`/`awk`/`grep` gymnastics. 2. **Behavior inventory** — list what the script actually does: inputs, env vars, side effects, exit codes, and any subtle Bash semantics (word splitting, `set -e` early exits, trap cleanup) that must be preserved. 3. **Map the constructs** — give a translation table: `set -euo pipefail` → exceptions + explicit checks; `trap cleanup EXIT` → `try/finally` or context managers; pipelines → `subprocess.run` with lists; `$(cmd)` → captured stdout; globbing → `pathlib.Path.glob`; arrays → lists; associative arrays → dicts. 4. **Subprocess discipline** — replace shelled-out commands with `subprocess.run([...], check=True, capture_output=True, text=True)`; avoid `shell=True`; prefer native Python (pathlib, shutil, json, http) over forking when there's a stdlib equivalent. 5. **Structure** — `main()`, argparse for the flags, functions for the steps, logging instead of `echo`, real exceptions instead of `exit 1` scattered around. 6. **Equivalence checks** — for a few representative inputs, show the original Bash output and the Python output side by side to prove parity, including exit codes. 7. **Tests** — pytest covering the happy path, an error path, and one tricky edge case (a filename with spaces, empty input) that the old Bash likely mishandled. 8. **Migration plan** — how to roll out safely: run both in parallel, diff outputs, then cut over; keep the old script tagged in git. Output: (a) keep-or-migrate verdict, (b) construct mapping table, (c) the Python rewrite, (d) pytest tests, (e) side-by-side parity examples. Bias toward: stdlib over subprocess, explicit errors, proving behavioral parity before cutover.
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
-
Python pathlib Filesystem Refactor Prompt
Refactor brittle os.path and string-concatenated file handling into clean, cross-platform pathlib code — safe joins, globbing, existence checks, and atomic-friendly path operations.
-
Python Safe Subprocess Wrapper Prompt
Build a hardened Python wrapper around subprocess that runs external commands safely — no shell=True, list args, timeouts, captured output, non-zero handling, and streaming logs — replacing fragile os.system and shell-string calls.
-
Python Type Hints and mypy Strict Retrofit Prompt
Incrementally add type hints to an untyped Python automation script and get it passing mypy --strict — annotating signatures, taming Any, modeling Optional, and wiring a CI gate — without a risky big-bang rewrite.
-
Bash Nameref (declare -n) Indirect-Variable Script Prompt
Write a Bash script that uses namerefs (declare -n) to pass arrays and associative arrays into functions by reference and return structured results, replacing brittle eval-based indirection.
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.