PromptABCD
FeaturesLearnHow it worksUse casesFAQGuideBlogContext Blocks
Sign inGet started free
Sign inSign up
PromptABCD

A calm home for your best AI prompts. Save them once, find them in seconds, reuse them forever.

Product

  • Features
  • Chrome Extension
  • Free Courses
  • How it works
  • Use cases
  • Blog
  • Context Blocks
  • Export Anywhere
  • FAQ

Resources

  • User guide
  • Learn prompting
  • Sign in
  • Get started free

© 2026 PromptABCD. All rights reserved.

Privacy PolicyTerms and Conditions
Home/Blog/Coding with AI/AI Prompts for Writing Bash Scripts
Coding with AI

AI Prompts for Writing Bash Scripts

A generated bash script with an unquoted variable deleted the wrong directory. Learn AI prompts for writing bash scripts that fail safely and handle the sharp edges.

September 10, 2026·8 min read
ShareShare
⚡Featured Prompt— copy and use right now
Write a bash script to back up my project folder and
delete the old backups.

A developer asked an AI for a bash script to clean up a build directory. The script had one unquoted variable. When that variable was empty — because an earlier step failed — the line

rm -rf $BUILD_DIR/
became
rm -rf /
. It started deleting the root filesystem before someone hit Ctrl+C. One missing pair of quotes, one catastrophic bug. Bash is full of these sharp edges, and AI prompts for writing bash scripts have to account for them, because bash will do exactly what you typed, no matter how disastrous.

What Are AI Prompts for Writing Bash Scripts?

AI prompts for writing bash scripts are structured requests that get a model to generate shell scripts with bash's many footguns disarmed — proper quoting, strict error modes, and safe handling of the empty and unexpected inputs that turn a helpful script into a destructive one.

Bash is uniquely dangerous among common languages because it's permissive by default. Unquoted variables split on whitespace. Undefined variables expand to empty strings silently. A failed command doesn't stop the script unless you tell it to. Each of these defaults is a trap, and generated bash walks into them constantly unless the prompt says otherwise.

⚡ Pro tip: Always ask for

set -euo pipefail
at the top of generated bash scripts.
-e
exits on any error,
-u
treats undefined variables as errors instead of empty strings, and
-o pipefail
catches failures in pipelines. These three flags disarm a huge share of bash's footguns in one line.

Why It Matters

The consequences of a bash bug are often immediate and irreversible. Unlike a web app bug that shows a broken page, a bash bug can delete files, overwrite data, or misconfigure a server in the time it takes to press Enter. And bash scripts often run with elevated permissions — as deployment scripts, cron jobs, or setup automation — which multiplies the damage a bug can do.

The intro story isn't rare. The unquoted-variable-becomes-

rm -rf /
bug is one of the most famous in the business precisely because it keeps happening. Bash's whitespace splitting and silent empty expansion mean that a variable you assumed had a value, but didn't, can transform a safe command into a destructive one. The defaults actively work against you.

Getting bash right compounds because bash scripts tend to stick around. That "temporary" deploy script runs for three years. A script written safely — quoted, strict-mode, error-handled — runs quietly the whole time. A script written carelessly is a landmine waiting for the one edge case that sets it off, usually at the worst possible moment.

Prompting for Safe Bash

The core of safe bash prompting is asking for the protections bash doesn't give by default.

Here's a weak bash prompt and its risk:

Write a bash script to back up my project folder and
delete the old backups.

"Delete the old backups" with no safety framing is how you get an unquoted

rm
that goes wrong. Now the safe version:

Write a bash script to back up ~/project to
~/backups/project-TIMESTAMP. Requirements:
- Start with set -euo pipefail
- Quote ALL variable expansions ("$var", not $var)
- Verify source exists before backing up; exit if not
- For deleting old backups, keep the 5 newest, and print
  what will be deleted before deleting (with a confirm
  prompt or --force flag)
- Never construct rm paths from possibly-empty variables
- Trap errors and print a clear message with the line number
Explain each safety measure in a comment.

What this does: It turns on strict mode, requires quoting, guards the destructive

rm
behind a confirmation, and specifically forbids the empty-variable-in-rm pattern that caused the intro disaster.

⚡ Pro tip: For any

rm
in a generated script, ask the model to guard against the empty-variable case explicitly. A check like
[[ -n "$dir" ]]
before an
rm -rf "$dir"
prevents the empty-expansion catastrophe. It's two lines that can save your filesystem.

Prompting for Portability and Clarity

Beyond safety, generated bash often has portability and readability issues worth prompting against.

Bash scripts that work on Linux sometimes break on macOS because of differences in tools like

sed
and
date
. If you need cross-platform scripts, say so:

This needs to run on both macOS and Linux. Avoid GNU-only
flags on sed/date, or detect the OS and branch. Note any
command that behaves differently across the two.

What this does: It surfaces the tool differences that silently break "portable" scripts, so you find out at write time instead of when a teammate on a different OS runs it.

A platform engineer I know always adds the cross-platform note because his team is split between Mac laptops and Linux servers, and the

sed -i
difference alone used to cause a support ticket a month until they started prompting for it.

⚡ Pro tip: Ask the model to add a

usage()
function and helpful error messages. Bash scripts are often run by people who didn't write them, and a script that prints how to use it when called wrong is far kinder than one that fails cryptically. Clarity is a safety feature too.

Common Mistakes

The biggest mistake is running a generated bash script with elevated permissions before reading it line by line. Bash does exactly what's written, instantly, and

sudo
removes your safety net. Read every destructive command before you run it, especially anything with
rm
,
mv
, or redirection into a file.

⚠️ Common mistake: Trusting unquoted variable expansions in generated bash. Unquoted

$var
splits on whitespace and expands empty variables to nothing, which is how safe-looking commands turn destructive. Always require quoted expansions, and scan generated scripts for any bare
$variable
that should be
"$variable"
.

Another frequent error is ignoring exit codes. A script that doesn't check whether a command succeeded will happily continue after a failure, compounding the problem. Strict mode (

set -e
) helps, but explicit checks on critical commands help more.

A subtler mistake is building file paths by concatenating variables without validation. If any part is empty or unexpected, you get a path pointing somewhere dangerous. Validate the pieces before using them in destructive operations, and never let an unchecked variable land inside an

rm
.

Conclusion

AI prompts for writing bash scripts succeed when they explicitly disarm bash's footguns — strict mode, quoted expansions, guarded destructive commands, and clear error handling. Bash won't protect you by default, so the prompt has to do the protecting.

The developers who write bash they can trust with

sudo
aren't braver; they've made safety the default in how they prompt. PromptABCD is where those defaults live — save your safe-bash template with
set -euo pipefail
and quoting rules baked in, keep a cross-platform variant, and reuse them on every script. The unquoted-variable disaster that starts this post becomes impossible when strict mode and quoting are baked into every prompt you send. Add
set -euo pipefail
to your next bash prompt, and make it the last time you write a shell script without a safety net.

One habit ties all of this together: read generated bash like it's trying to hurt you, because in a sense it is. Bash's permissiveness means the model can hand you a script that looks fine and contains a subtle footgun — an unquoted variable, a missing existence check, a pipeline whose failure goes unnoticed. Slowing down to read every destructive line, and asking "what if this variable is empty?" at each one, is the single most valuable review habit in shell scripting. It takes a minute and prevents the disasters that make bash infamous.

⚡ Pro tip: For any bash script you'll run more than once, ask the model to make it pass shellcheck cleanly. Shellcheck is a static analyzer that catches most common bash mistakes automatically, and prompting for shellcheck-clean code means the model applies those rules while writing rather than leaving you to find the problems after. It's like having a bash expert review every line before you run it.

It's worth internalizing that bash's danger and its usefulness come from the same source: it does precisely what you say, immediately, with full system access. That power is why bash remains the glue of the computing world decades after fancier languages arrived, and it's why a careless line can be catastrophic. Respecting bash means treating every generated script as something to read carefully rather than run blindly, and building the safety in at the prompt so the model does most of the careful thinking for you. Do that, and bash becomes the reliable workhorse it's meant to be instead of the footgun it's infamous for. Make strict mode and quoting your defaults, read every destructive line, and bash will serve you reliably for years instead of surprising you once in a way you never forget. That trade — a little care up front for a lot of safety over time — is one every experienced engineer eventually learns to make. The safety net you build into your prompts today is the disaster you never have to explain tomorrow. Careful bash is quiet bash, and quiet bash is the kind you can finally stop worrying about entirely.

bashshell scriptinglinuxdevopsai promptsautomation

Continue Reading

AI Prompts for Tailwind CSS
Coding with AI

AI Prompts for Tailwind CSS

Most Tailwind AI advice is wrong: it treats Tailwind like inline styles. Learn AI prompts for Tailwind CSS that produce clean, reusable, design-consistent components.

September 10, 2026·8 min read
AI Prompts for CSS and Styling
Coding with AI

AI Prompts for CSS and Styling

Why does AI-generated CSS look right until you resize the window? Learn AI prompts for CSS and styling, torn down from fragile to responsive and maintainable.

September 10, 2026·8 min read
AI Prompts for Writing Regex
Coding with AI

AI Prompts for Writing Regex

Picture a regex that passed your test cases and quietly failed on real data. This case study shows AI prompts for writing regex that hold up outside the sample set.

September 10, 2026·8 min read

Save the prompts from this post

PromptABCD is a free prompt manager. Paste, organize, and reuse your best AI prompts — no more hunting through chat history.

Start free →
← PreviousAI Prompts for Tailwind CSSNext →What Is a CLI AI Agent? A Developer's Plain-English Guide
Share this post:
ShareShare