Write Bash Scripts with AI
To write a bash script with AI, describe the task to @vustbot in Telegram — inputs, target OS, what should happen on failure — and ask for a commented script with strict-mode error handling. You copy the result into your terminal yourself: the bot writes the script, it never runs anything on your machine.
Bash is the language everyone uses and nobody remembers — the find flags, the quoting rules, the trap syntax. That makes it a strong fit for a chat model, as long as you state the environment and read the result before running it.
What the AI does in this scenario
- Task description → commented, strict-mode script in one message
- Names its assumptions: GNU vs BSD tools, required binaries, root or not
- Regenerate with a second model (GPT-5-class, Claude, Gemini) when the first take is clumsy
- Explains any line back to you in the same chat before you run it
- Per-action Balance — no CLI-tool subscription for a script a month
Worked example: write bash scripts with ai
01Input — what you paste
Task: rotate logs for /var/log/myapp on Ubuntu — gzip *.log files older than 7 days, delete .log.gz archives older than 30 days, print everything touched. Must be safe to re-run.
02Output — what comes back
#!/usr/bin/env bash set -euo pipefail LOG_DIR="/var/log/myapp" # compress logs untouched for 7+ days (the -name '*.log' filter keeps .gz files out of gzip) find "$LOG_DIR" -maxdepth 1 -name '*.log' -mtime +7 -print -exec gzip {} \; # drop archives older than 30 days find "$LOG_DIR" -maxdepth 1 -name '*.log.gz' -mtime +30 -print -delete
How to write bash scripts with ai — step by step
- 1Describe the task and the environment
One message to @vustbot: what the script must do, the OS (Ubuntu vs macOS changes the find and sed flags), and what should happen on failure. Environment omitted is the #1 source of broken output.
- 2Ask for strict mode and comments
Request "set -euo pipefail, a comment per non-obvious line, and no silent failures". A script that dies loudly at the first bad step is debuggable; one that half-runs is not.
- 3Read it, dry-run it, then run it
Paste any line you don't understand back into the chat and ask what it does. Run with echoed commands or on a scratch directory first — the bot cannot test the script for you, so this step is yours.
AI vs doing it manually
Honest take: for the classic one-liners — "find files older than N days", "loop over lines in a file" — a Stack Overflow answer or your own snippets file is often faster and already battle-tested; generation earns nothing there. AI wins when the task is a combination no one has posted verbatim: your directory layout, your retention rules, your logging format, stitched into one script with error handling you'd otherwise skip. It also wins the explain-back step — asking "what does this trap line do" beats reverse-engineering someone's 2014 forum answer.
The prompt to copy
Write a bash script that [TASK]. Target OS: [UBUNTU/MACOS/ALPINE]. Inputs: [FILES/ARGS/ENV VARS]. On failure: [STOP AND EXIT/LOG AND CONTINUE]. Requirements: set -euo pipefail; quote all variable expansions; comment each non-obvious line; list any assumptions (required binaries, permissions) at the top.
Frequently asked questions
How do I make an AI-written bash script fail loudly instead of half-running?
Ask for it explicitly: "use set -euo pipefail and exit non-zero on any failure". Without that request, models often produce optimistic scripts that keep going after a failed command — the single most dangerous default in generated bash. The prompt template on this page includes the rule.
Why does the generated script use flags that don't exist on my Mac?
GNU vs BSD userland: macOS ships BSD find, sed and date, whose flags differ from the Linux versions most training data assumes. Name your OS in the prompt — "target: macOS" — and the bot writes for the right toolset, or tells you which GNU tools to brew-install.
Can @vustbot run or test the bash script it wrote?
No — it is a chat, not a shell. It writes the script from your description and can explain or revise any line, but execution, dry-runs and shellcheck are on your machine. Treat its output like a snippet from a colleague: read it before you run it.
Related in Developers
Try it on your real task
The welcome bonus covers a first run — send the prompt above with your own facts and judge the output yourself.
Open @vustbot