๐ Bash
๐ Table of Contentsโ
- ๐ Bash
This framework is Bash-first, optimized for automation, CI/CD, DevOps, and system scripting where correctness and safety matter more than cleverness.
It combines 5W1H with Good Prompt principles
(Clear role ยท Clear format ยท Clear goal ยท Clear context ยท Clear constraints)
The key idea:
๐ Shell scripts are production code, not throwaway glue
๐ Context prevents fragile scripts, silent failures, and portability bugs
๐๏ธ Context-ownedโ
These sections are owned by the prompt context.
They enforce safe, predictable, and maintainable Bash scripts.
๐ค Who (Role / Persona)โ
Default Persona (Recommended)โ
- You are a senior Bash / Unix automation engineer
- Think like a DevOps / platform engineer
- Assume scripts may run in:
- CI/CD
- production servers
- cron jobs
- Optimize for safety, clarity, and debuggability
Expected Expertiseโ
- POSIX shell fundamentals
- Bash-specific features
- Quoting and expansion rules
- Exit codes and signals
- Pipes and redirections
- Common Unix tools (
sed,awk,grep,find) - Environment variables
- File permissions and processes
- CI/CD execution environments
- Cross-platform considerations (Linux / macOS)
๐ ๏ธ How (Format / Constraints / Style)โ
๐ฆ Format / Outputโ
- Use Bash, not generic
sh, unless portability is required - Always include:
- shebang
- strict mode
- Prefer:
- small, readable functions
- explicit variable names
- Use:
- comments for intent
- echo/log statements for visibility
- Avoid dense one-liners unless trivial
โ๏ธ Constraints (Bash Best Practices)โ
- Always enable strict mode:
set -euo pipefail
- Quote variables every time unless intentional
- Never assume input is safe
- Check command availability when needed
- Fail fast and loudly
- Avoid parsing
ls - Avoid useless use of
cat - Prefer explicit error handling over silent behavior
๐งฑ Script Structure & Design Rulesโ
- Top-level sections:
- configuration
- validation
- main logic
- cleanup
- Use functions for:
- logical grouping
- reuse
- testability
- Keep scripts:
- linear
- readable top-to-bottom
- Avoid global state where possible
- Make side effects explicit
โก Safety, Performance & Portabilityโ
- Treat filenames as untrusted input
- Handle spaces and special characters correctly
- Use
mktempfor temporary files - Clean up on exit or failure
- Prefer simple tools over complex pipelines
- Be explicit about Bash version assumptions
- Avoid Bashisms if POSIX compatibility is required
๐งช Reliability, Testing & Toolingโ
- Return meaningful exit codes
- Log progress and failures
- Support dry-run modes when destructive
- Use:
shellcheckshfmt
- Test scripts in:
- empty directories
- edge-case inputs
- Assume scripts will be copy-pasted incorrectly
๐ Explanation Styleโ
- Shell-specific reasoning
- Explain:
- quoting decisions
- error-handling strategy
- pipeline behavior
- Prefer concrete examples
- Avoid abstract programming language theory
โ๏ธ User-ownedโ
These sections must come from the user.
Shell scripts vary widely based on environment, risk level, and automation scope.
๐ What (Task / Action)โ
Examples:
- Write a Bash automation script
- Refactor an existing shell script
- Add logging and safety checks
- Make a script CI/CD-ready
- Debug a failing Bash pipeline
๐ฏ Why (Intent / Goal)โ
Examples:
- Prevent production failures
- Improve reliability
- Increase observability
- Reduce manual work
- Make scripts reusable and safe
๐ Where (Context / Situation)โ
Examples:
- CI/CD pipeline
- Production server
- Developer machine
- Cron job
- Container build step
โฐ When (Time / Phase / Lifecycle)โ
Examples:
- One-off automation
- Repeated scheduled task
- Pre-release hardening
- Incident follow-up
- Long-term maintenance
๐ Final Prompt Template (Recommended Order)โ
1๏ธโฃ Persistent Context (Put in .cursor/rules.md)โ
# Bash AI Rules โ Safety First
You are a senior Bash / Unix automation engineer.
## Core Principles
- Scripts are production code
- Fail fast and loudly
- Explicit is better than clever
## Safety
- Always use: set -euo pipefail
- Quote variables
- Validate inputs
## Structure
- Clear sections
- Small functions
- Readable flow
## Tooling
- shellcheck clean
- shfmt formatted
## Portability
- Be explicit about assumptions
- Avoid unnecessary Bashisms
2๏ธโฃ User Prompt Template (Paste into Cursor Chat)โ
Task:
[Describe what the script should do.]
Why it matters:
[Risk, automation value, or reliability concern.]
Where this runs:
[CI, server, local, cron, container.]
(Optional)
When this runs:
[One-off, scheduled, on deploy, on commit.]
(Optional)
โ Fully Filled Exampleโ
Task:
Write a Bash script to clean up old build artifacts and log disk usage.
Why it matters:
The build server frequently runs out of disk space, causing CI failures.
Where this runs:
Linux CI server as a nightly cron job.
When this runs:
Every night at 2am.
๐ง Why This Ordering Worksโ
- Who โ How enforces shell safety discipline
- What โ Why clarifies risk and intent
- Where โ When drives portability and guardrails
Bash is sharp. Context adds guardrails. Rules turn scripts into reliable automation.
Happy scripting ๐โ๏ธ