Best AI Prompts for Bash Scripting
AI is useful for Bash scripting when you ask for small, reviewable changes and insist on safety checks. These prompts are built for Linux admin work, automation, log checks, backups and troubleshooting.
Rules for using AI with Bash scripts
Good Bash prompts are specific. Tell the AI what the script should do, what operating system it will run on, what commands are available and what must not happen.
- Ask for a dry-run mode before destructive actions.
- Ask for comments only where they help understanding.
- Ask for
set -euo pipefailonly when it suits the script. - Ask for input validation, logging and clear error messages.
- Review every command before running it as root.
Create a new Bash script
Write a Bash script for AlmaLinux that checks disk usage for all mounted filesystems.
Requirements:
- Warn if usage is over 85%.
- Print filesystem, used percentage and mount point.
- Exit with code 1 if any filesystem is over the threshold.
- Avoid checking tmpfs and devtmpfs.
- Include comments for the main sections only.
- Explain how to test it safely.
This works well because it includes the platform, task, output format, threshold, exclusions and exit-code behaviour.
Debug an existing script
I have this Bash script. Please review it for syntax errors, unsafe quoting, missing error handling and edge cases.
Do not rewrite everything unless needed.
Return:
1. Problems found
2. Minimal fixed version
3. Explanation of each fix
4. Commands I can run to test it
SCRIPT:
[paste script here]
This tells the AI to perform a targeted review instead of turning a small fix into a novel with semicolons.
Make a Bash script safer
Improve this Bash script for safety.
Requirements:
- Quote variables correctly.
- Add a dry-run option.
- Avoid deleting files unless --confirm is passed.
- Log actions to /var/log/example-script.log.
- Keep the script POSIX-friendly where possible.
- Explain any command that could be destructive.
This is especially useful for cleanup scripts, backup rotation, log deletion and permission fixes.
Useful AI prompts for server admin Bash
| Task | Prompt idea |
|---|---|
| Website uptime | Ask for a script that checks a list of URLs, records HTTP status, measures response time and exits non-zero on failures. |
| Log search | Ask for a script that scans Apache logs for 404s, 500s, top IPs and suspicious user agents. |
| Backups | Ask for an rsync backup script with dry-run, excludes, logging and retention notes. |
| Service monitoring | Ask for a systemctl script that checks service state and prints journalctl lines for failed services. |
| Permissions audit | Ask for a script that finds world-writable files but excludes known safe paths. |
Checklist before running AI-generated Bash
- Does it use
rm,find -delete,chmod -R,chown -Ror database commands? - Does it quote variables like
"$file"? - Does it handle spaces in filenames?
- Does it have a dry-run or preview mode?
- Does it log what it changed?
- Can you test it on a small sample directory first?