Write Professional and Robust Bash Scripts
A comprehensive prompt to generate robust, professional Bash scripts with error handling, argument parsing and built-in best practices.
Paste in your AI
Paste this prompt in ChatGPT, Claude or Gemini and customize the variables in brackets.
You are an expert in Bash scripting and Linux/macOS system administration. Write a complete Bash script to accomplish the following task: [task description].
Execution context:
- Target system: [target system e.g. Ubuntu 22.04, macOS, CentOS, generic POSIX]
- Desired robustness level: [level e.g. quick script, production, mission-critical]
The script must:
- Start with an appropriate shebang and a header documenting the purpose, author, and date
- Use
set -euo pipefailfor strict error handling - Define configuration variables at the top of the file, clearly commented
- Include a
usage()function that displays help with the available options - Parse arguments with
getoptsorgetoptdepending on complexity - Implement error handling with explicit messages to stderr
- Use a trap to clean up temporary files on exit
- Check prerequisites (required commands, permissions, input files)
- Use functions for each distinct logical block
- Include a verbose/debug mode that can be enabled with a flag
- Log important actions with timestamps
- Return appropriate exit codes (0 success, 1 usage error, 2 execution error)
For each section of the script, add comments explaining the reasoning. At the end, provide:
- An example of use with the exact commands
- Common pitfalls to avoid for this type of script
- Suggestions for improvement (cron, systemd timer, CI/CD integration)
Personalize this prompt with Léa · AI assistant
Léa rewrites this prompt for your job and your exact goal — 3 quick questions.
Why this prompt works
This prompt is designed to produce production-quality Bash scripts. By specifying the precise task and the target system, you get a script tailored to your environment. The prompt enforces essential best practices like set -euo pipefail, trap for cleanup, and argument parsing — elements often neglected in ad-hoc scripts. To use it effectively, be as specific as possible when describing the task. For example, instead of "back up files", write "daily backup of /var/www to an S3 bucket with 30-day rotation and Slack notification on failure". The richer the context, the more relevant and complete the script will be. You can adjust the robustness level to your needs: a quick script for prototyping will be more concise, while a mission-critical script will include advanced logging, locks to prevent simultaneous execution, and fine-grained signal handling. Feel free to iterate by requesting additions like external configuration file support, systemd integration, or automated tests with bats.
Use Cases
Expected Output
A complete, commented Bash script, ready to use, with shebang, error handling, argument parsing, modular functions, usage examples and improvement recommendations.
Improve this prompt
Run this prompt through the Optimizer to strengthen its context, constraints and expected format.
Improve this prompt with the OptimizerComments
Be the first to comment on this prompt.
📬 Get new prompts every week
Join our newsletter and never miss a prompt.