AI and Bash

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.

On this page
Before you paste

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 pipefail only when it suits the script.
  • Ask for input validation, logging and clear error messages.
  • Review every command before running it as root.
Never run AI-generated Bash against live customer data without reading it first. Bash is powerful. Bash with root access is a chainsaw wearing sunglasses.
Prompt template

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.

Debugging

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.

Safety

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.

Linux admin examples

Useful AI prompts for server admin Bash

TaskPrompt idea
Website uptimeAsk for a script that checks a list of URLs, records HTTP status, measures response time and exits non-zero on failures.
Log searchAsk for a script that scans Apache logs for 404s, 500s, top IPs and suspicious user agents.
BackupsAsk for an rsync backup script with dry-run, excludes, logging and retention notes.
Service monitoringAsk for a systemctl script that checks service state and prints journalctl lines for failed services.
Permissions auditAsk for a script that finds world-writable files but excludes known safe paths.
Checklist

Checklist before running AI-generated Bash

  • Does it use rm, find -delete, chmod -R, chown -R or 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?
Next steps

Practise the Bash skills behind the prompts

$ practise_next --topic bash

Practise this next

Turn the guide into practice with a related quiz, builder, cheat sheet or learning path.