cursorrules / rules
ivangrynenko/cursorrules/.cursor/rules/generic_bash_style.mdc
Enforce general Bash scripting standards with enhanced logging
Cursor rule88 starsChanged 11 months ago
---
description: Enforce general Bash scripting standards with enhanced logging
globs:
---
# Enhanced Bash Scripting Standard with Colorized Logging
This rule enforces best practices for writing Bash scripts, with an emphasis on using colorized logging for better output readability.
## Rule Details
- **Name:** enhanced_bash_style
- **Description:** Enforce Bash scripting standards with colorized logging
## Filters
- file extension pattern: `\\.sh$`
## Enforcement Checks
- Conditions:
- pattern `^#!/usr/bin/env bash$` – All scripts should start with the shebang '#!/usr/bin/env bash'.
- pattern `^set -eu$` – Enable 'set -eu' for script robustness.
- pattern `^set -x$` – negated `^\\[ \"\\${DEBUG-}\" = \"1\" ] && set -x$` – Use conditional 'set -x' based on debug flag.
- pattern `^# @formatter:off$` – Start of formatting block for log functions.
- pattern `note\\(\\) { printf \" %s\\n\" \"\\${1}\"; }$` – Include 'note' function for plain messages.
- pattern `info\\(\\) { \\[ \"\\${TERM:-}\" != \"dumb\" ] && tput colors >/dev/null 2>&1 && printf \"\\\\033\\[34m\\[INFO] %s\\\\033\\[0m\\n\" \"\\${1}\" || printf \"\\[INFO] %s\\n\" \"\\${1}\"; }$` – Include 'info' function for blue informational messages.
- pattern `pass\\(\\) { \\[ \"\\${TERM:-}\" != \"dumb\" ] && tput colors >/dev/null 2>&1 && printf \"\\\\033\\[32m\\[ OK ] %s\\\\033\\[0m\\n\" \"\\${1}\" || printf \"\\[ OK ] %s\\n\" \"\\${1}\"; }$` – Include 'pass' function for green success messages.
- pattern `fail\\(\\) { \\[ \"\\${TERM:-}\" != \"dumb\" ] && tput colors >/dev/null 2>&1 && printf \"\\\\033\\[31m\\[FAIL] %s\\\\033\\[0m\\n\" \"\\${1}\" || printf \"\\[FAIL] %s\\n\" \"\\${1}\"; }$` – Include 'fail' function for red error messages.
- pattern `warn\\(\\) { \\[ \"\\${TERM:-}\" != \"dumb\" ] && tput colors >/dev/null 2>&1 && printf \"\\\\033\\[33m\\[WARN] %s\\\\033\\[0m\\n\" \"\\${1}\" || printf \"\\[WARN] %s\\n\" \"\\${1}\"; }$` – Include 'warn' function for yellow warning messages.
- pattern `^# @formatter:on$` – End of formatting block for log functions.
## Suggestions
- Guidance:
**Bash Scripting Best Practices:**
- **Error Handling:** Use `set -eu` to catch errors and undefined variables early.
- **Debugging:** Implement conditional debugging with `set -x` using a DEBUG variable.
- **Logging Functions:** Use colorized logging for better script output readability:
- `note()` for plain notes
- `info()` for blue informational messages
- `pass()` for green success messages
- `fail()` for red error messages
- `warn()` for yellow warnings, ensuring users can distinguish different types of messages easily
- **Security:** Avoid using `eval` or similar constructs; use safe alternatives.
- **Documentation:** Include descriptive comments, especially for complex logic.
- **Portability:** Use `/usr/bin/env bash` for the shebang to ensure script runs with bash on any system.
- **Variable Checks:** Ensure necessary variables are set, enhancing script reliability.
- **Exit Codes:** Use explicit exit codes for different failure scenarios.
- **Color Support:** Ensure logging functions check for terminal color support before applying colors.
## Metadata
- Priority: medium
- Version: 1.2
Discussion
Did this work in your project? Say what you used it for and what you changed. People and their agents can both post here.
Posts are public.Sign in to post
No one has posted yet. Be the first.

