5 ChatGPT Prompts for Bash Scripts

Asking a model for “a bash script that does X” is the slowest way to get a working script. You get a hundred lines that run fine on the happy path and delete the wrong directory the first time a variable is empty. These five ChatGPT prompts for bash scripts work the other way around: pin the constraints and the failure behavior first, then generate, then review.

Before you start: the variables

Variable What to put in it
[task] One sentence on what the script must accomplish, including the input and the output.
[shell and OS] bash version and the platform it runs on, since macOS and Linux differ.
[input] Where the data comes from: file path, argument, pipe, or environment variable.
[safety constraint] What must never happen, such as deleting outside a target directory.
[runtime] Interactive terminal, cron job, or CI step, because prompts and paths differ.

The prompts

1. Skeleton that fails loudly

Write the skeleton of a bash script for [task] on [shell and OS]. Include strict mode, a cleanup trap that runs on exit and error, and a check that required commands exist. Leave function bodies as TODO comments. No logic yet.

What it does: Sets up the parts people forget until the script fails halfway through.

How to use: Replace [task] and the platform. Confirm the cleanup trap before adding any logic.

2. Arguments and usage text

Add argument parsing to this script: [skeleton]. Accept [flags] with defaults, reject unknown flags with a usage message, and exit non-zero on missing required input. Keep it readable without a parser library.

What it does: Makes the script usable by someone who did not write it.

How to use: Replace the skeleton and flags. Test with no arguments first, then with a bad flag.

3. Dry run before anything destructive

Add a --dry-run flag to this script: [script]. In dry-run mode, print every file operation it would perform and change nothing. Enforce this constraint: [safety constraint]. Show the guard that makes accidental execution impossible.

What it does: Lets you see the blast radius before it exists.

How to use: Replace the script and constraint. Run dry-run on real data, read the output, then run for real.

4. Logging that tells you where it stopped

Add error handling and logging to this script: [script]. Log the step name, the command, and the exit code on failure. Write to stderr for errors and stdout for progress. Keep timestamps optional so cron logs stay readable.

What it does: Turns “it broke” into a line number and a command.

How to use: Replace the script. Trigger a deliberate failure once to confirm the log is useful.

5. Review before it touches production

Review this bash script for [runtime]: [script]. Check quoting, word splitting, unset variables, exit codes, and anything that behaves differently under cron. Rank the findings by damage if triggered.

What it does: Catches the class of bug that only shows up in an unattended run.

How to use: Replace the runtime and script. Fix the top finding before scheduling anything.

A quick worked example

Suppose [task] is “move screenshots older than a week from the desktop into an archive folder”. Prompt 1 returns strict mode plus a trap that logs the exit code. Prompt 2 adds --days with a default of 7 and --target with no default, so the script refuses to run without a destination. Prompt 3 prints each mv it would perform and stops if the path resolves outside the archive. Prompt 4 logs move and archive-cleanup as step names. Prompt 5 flags one unquoted variable in the loop. You fix that, run it once with --dry-run, then let cron have it.

Keep going

GuPrompt keeps every prompt copy-paste ready. No account needed.

Leave a Comment