Shell Scripts Best Practices (Community)
Comprehensive best practices guide for shell scripting, designed for AI agents and LLMs. Contains 49 rules across 9 categories, prioritized by impact from critical (safety, portability) to incremental (style). Each rule includes detailed explanations, real-world examples comparing incorrect vs. correct implementations, and specific impact metrics.
When to Apply
Reference these guidelines when:
- Writing new bash or POSIX shell scripts
- Reviewing shell scripts for security vulnerabilities
- Debugging scripts that fail silently or behave unexpectedly
- Porting scripts between Linux, macOS, and containers
- Optimizing shell script performance
- Setting up CI/CD pipelines with shell scripts
Rule Categories by Priority
Quick Reference
1. Safety & Security (CRITICAL)
safety-command-injection[blocked] - Prevent command injection from user inputsafety-eval-avoidance[blocked] - Avoid eval for dynamic commandssafety-absolute-paths[blocked] - Use absolute paths for external commandssafety-temp-files[blocked] - Create secure temporary filessafety-suid-forbidden[blocked] - Never use SUID/SGID on shell scriptssafety-argument-injection[blocked] - Prevent argument injection with double dash
2. Portability (CRITICAL)
port-shebang-selection[blocked] - Choose shebang based on portability needsport-avoid-bashisms[blocked] - Avoid bashisms in POSIX scriptsport-printf-over-echo[blocked] - Use printf instead of echo for portabilityport-export-syntax[blocked] - Use portable export syntaxport-test-portability[blocked] - Use portable test constructs
3. Error Handling (HIGH)
err-strict-mode[blocked] - Use strict mode for error detectionerr-exit-codes[blocked] - Use meaningful exit codeserr-trap-cleanup[blocked] - Use trap for cleanup on exiterr-stderr-messages[blocked] - Send error messages to stderrerr-pipefail[blocked] - Use pipefail to catch pipeline errorserr-check-commands[blocked] - Check command success explicitlyerr-shellcheck[blocked] - Use ShellCheck for static analysiserr-debug-tracing[blocked] - Use debug tracing with set -x and PS4
4. Variables & Data (HIGH)
var-use-arrays[blocked] - Use arrays for lists instead of stringsvar-local-scope[blocked] - Use local for function variablesvar-naming-conventions[blocked] - Follow variable naming conventionsvar-readonly-constants[blocked] - Use readonly for constantsvar-default-values[blocked] - Use parameter expansion for defaults
5. Quoting & Expansion (MEDIUM-HIGH)
quote-always-quote-variables[blocked] - Always quote variable expansionsquote-dollar-at[blocked] - Use "$@" for argument passingquote-command-substitution[blocked] - Quote command substitutionsquote-brace-expansion[blocked] - Use braces for variable clarityquote-here-documents[blocked] - Use here documents for multi-line stringsquote-glob-safety[blocked] - Control glob expansion explicitly
6. Functions & Structure (MEDIUM)
func-main-pattern[blocked] - Use main() function patternfunc-single-purpose[blocked] - Write single-purpose functionsfunc-return-values[blocked] - Use return values correctlyfunc-documentation[blocked] - Document functions with header commentsfunc-avoid-aliases[blocked] - Prefer functions over aliases
7. Testing & Conditionals (MEDIUM)
test-double-brackets[blocked] - Use [[ ]] for tests in bashtest-arithmetic[blocked] - Use (( )) for arithmetic comparisonstest-explicit-empty[blocked] - Use explicit empty/non-empty string teststest-file-operators[blocked] - Use correct file test operatorstest-case-patterns[blocked] - Use case for pattern matching
8. Performance (LOW-MEDIUM)
perf-builtins-over-external[blocked] - Use builtins over external commandsperf-avoid-subshells[blocked] - Avoid unnecessary subshellsperf-process-substitution[blocked] - Use process substitution for temp filesperf-read-files[blocked] - Read files efficientlyperf-parameter-expansion[blocked] - Use parameter expansion for string operationsperf-batch-operations[blocked] - Batch operations instead of loops
9. Style & Formatting (LOW)
style-indentation[blocked] - Use consistent indentationstyle-file-structure[blocked] - Follow consistent file structurestyle-comments[blocked] - Write useful comments
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions [blocked] - Category structure and impact levels
- Rule template [blocked] - Template for adding new rules


