Write a Bash script for [TASK TO AUTOMATE]. It will run on [TARGET ENVIRONMENT] using these inputs and files: [INPUTS AND FILE LOCATIONS]. The required behavior, output, and success criteria are [EXPECTED BEHAVIOR]. Safety constraints and failure cases are [SAFETY REQUIREMENTS]. Target Bash compatible with the stated environment. Begin with a short usage block and a clear list of assumptions. Use `#!/usr/bin/env bash` and choose shell options deliberately; explain any use of `set -euo pipefail` because it can change error behavior. Parse arguments predictably, validate required values before changing anything, quote variable expansions, use arrays for lists, and use `--` before path operands where relevant. Send errors to stderr and exit nonzero on failure. For operations that overwrite, delete, move, upload, or change remote state, include a `--dry-run` mode by default and require an explicit confirmation flag such as `--apply` before making changes. Avoid `eval`, unbounded glob deletion, parsing `ls`, and assumptions about whitespace-free filenames. Make paths configurable rather than hard-coding personal directories. Produce:
1. The complete script in one code block. 2. A short explanation of arguments, dependencies, outputs, and exit behavior. 3. Three example commands, including dry-run. 4. A test checklist covering normal input, empty input, paths with spaces, missing tools, and a partial-failure case. Do not claim the script was executed. Do not invent commands, APIs, paths, credentials, or operating-system behavior; mark anything environment-dependent. Before answering, trace every destructive or state-changing command and confirm it is guarded as requested. Also check that all variables and command substitutions are safely quoted. Ask up to 3 clarifying questions only if a required input is missing.
Fill in
| Placeholder | What to enter | Example |
|---|---|---|
| [TASK TO AUTOMATE] | Describe the exact repetitive command-line task and desired end state. | Archive CSV exports older than 30 days into monthly tar.gz files, then remove only successfully archived source files. |
| [TARGET ENVIRONMENT] | State the operating system, Bash version if known, and available tools. | macOS 14 with Bash 5 installed through Homebrew; tar and find are available. |
| [INPUTS AND FILE LOCATIONS] | List arguments, directories, file patterns, and expected input formats. | Required --source DIR and --archive-dir DIR; CSV files may contain spaces; process only files directly inside source. |
| [EXPECTED BEHAVIOR] | Define what the script should do, what it should print, and how success is determined. | Group files by modification month, create one archive per month, print each planned action, and return nonzero if any archive fails. |
| [SAFETY REQUIREMENTS] | List actions that need dry-run, confirmation, backups, exclusions, or special handling. | Default to --dry-run; deletion requires --apply; never touch files newer than 30 days or existing archives without --overwrite. |
How to use
- Describe the input format and operating system, because Bash utilities differ between macOS and Linux.
- State every operation that can change or delete data and require a dry-run path first.
- Run the generated script against a disposable test directory before using production files.
- Follow up with: “Add Bats tests for the five cases in the test checklist without changing production behavior.”
Variations
Bash script review
Use when you already have a script and need a safety and portability review.
Review this Bash script: [SCRIPT]. It runs on [ENVIRONMENT] and is intended to [PURPOSE]. Identify correctness bugs, unsafe expansion or deletion paths, portability issues, misleading error handling, and missing input validation. Return findings ranked by severity, then provide a minimally changed corrected version and a test plan. Do not assume it has been run. Before answering, trace every path that writes or deletes and check behavior with spaces, empty variables, and failed commands.
Command-line wrapper
Use to turn a repeated command sequence into a documented CLI tool.
Write a Bash command-line wrapper for [UNDERLYING COMMAND OR TOOL]. Required user workflow: [WORKFLOW]. Environment and dependencies: [ENVIRONMENT]. Produce a complete script with help text, named options, validation, clear stderr errors, and example invocations. Preserve argument boundaries using arrays; do not use eval or string-built shell commands. Include --dry-run and --apply if the underlying command changes state. Check that user input cannot be reinterpreted as shell syntax.
Bash debugging prompt
Use when a script fails and you need a focused diagnosis before changing it.
Diagnose this Bash failure. Script: [SCRIPT]. Exact command: [COMMAND]. Full output and exit code: [ERROR OUTPUT]. Environment: [ENVIRONMENT]. Explain the most likely cause, what evidence supports it, two safe commands to confirm it, and the smallest fix. Then show the revised relevant lines only. Do not invent logs or say the fix was tested. Check that your proposed diagnostic commands do not modify files or remote state.
Tips
- Specify the shell and platform; macOS commonly ships older Bash and BSD variants of utilities with different flags.
- Treat every filename as capable of containing spaces, leading dashes, and shell metacharacters; arrays and quoted expansions prevent many failures.
- Make destructive behavior opt-in and make dry-run output describe exactly what would happen.
- Test failure paths, not just happy paths: missing dependencies, inaccessible directories, empty matches, interrupted runs, and one bad file in a batch.
FAQ
Can ChatGPT write a Bash script I can run directly?
It can draft one, but you should test it in a temporary directory and inspect every command that changes data, permissions, or remote systems.
Should every Bash script use set -euo pipefail?
Not automatically. These options are useful, but each has edge cases; the script should handle expected nonzero statuses intentionally.
How do I make a Bash script safer before deleting files?
Require explicit paths, validate them, print a dry-run plan, and require a separate confirmation flag before deletion.