File contents 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
Priority
Category
Impact
Prefix
Rules
1
Safety & Security
CRITICAL
safety-
6
2
Portability
CRITICAL
port-
5
3
Error Handling
HIGH
err-
8
4
Variables & Data
HIGH
var-
5
5
Quoting & Expansion
MEDIUM-HIGH
quote-
6
6
Functions & Structure
MEDIUM
func-
5
7
Testing & Conditionals
MEDIUM
test-
5
8
Performance
LOW-MEDIUM
perf-
6
9
Style & Formatting
LOW
style-
3
Quick Reference
1. Safety & Security (CRITICAL)
safety-command-injection - Prevent command injection from user input
safety-eval-avoidance - Avoid eval for dynamic commands
safety-absolute-paths - Use absolute paths for external commands
safety-temp-files - Create secure temporary files
safety-suid-forbidden - Never use SUID/SGID on shell scripts
safety-argument-injection - Prevent argument injection with double dash
2. Portability (CRITICAL)
port-shebang-selection - Choose shebang based on portability needs
port-avoid-bashisms - Avoid bashisms in POSIX scripts
port-printf-over-echo - Use printf instead of echo for portability
port-export-syntax - Use portable export syntax
port-test-portability - Use portable test constructs
3. Error Handling (HIGH)
err-strict-mode - Use strict mode for error detection
err-exit-codes - Use meaningful exit codes
err-trap-cleanup - Use trap for cleanup on exit
err-stderr-messages - Send error messages to stderr
err-pipefail - Use pipefail to catch pipeline errors
err-check-commands - Check command success explicitly
err-shellcheck - Use ShellCheck for static analysis
err-debug-tracing - Use debug tracing with set -x and PS4
4. Variables & Data (HIGH)
var-use-arrays - Use arrays for lists instead of strings
var-local-scope - Use local for function variables
var-naming-conventions - Follow variable naming conventions
var-readonly-constants - Use readonly for constants
var-default-values - Use parameter expansion for defaults
5. Quoting & Expansion (MEDIUM-HIGH)
quote-always-quote-variables - Always quote variable expansions
quote-dollar-at - Use "$@" for argument passing
quote-command-substitution - Quote command substitutions
quote-brace-expansion - Use braces for variable clarity
quote-here-documents - Use here documents for multi-line strings
quote-glob-safety - Control glob expansion explicitly
6. Functions & Structure (MEDIUM)
func-main-pattern - Use main() function pattern
func-single-purpose - Write single-purpose functions
func-return-values - Use return values correctly
func-documentation - Document functions with header comments
func-avoid-aliases - Prefer functions over aliases
7. Testing & Conditionals (MEDIUM)
test-double-brackets - Use [[ ]] for tests in bash
test-arithmetic - Use (( )) for arithmetic comparisons
test-explicit-empty - Use explicit empty/non-empty string tests
test-file-operators - Use correct file test operators
test-case-patterns - Use case for pattern matching
8. Performance (LOW-MEDIUM)
perf-builtins-over-external - Use builtins over external commands
perf-avoid-subshells - Avoid unnecessary subshells
perf-process-substitution - Use process substitution for temp files
perf-read-files - Read files efficiently
perf-parameter-expansion - Use parameter expansion for string operations
perf-batch-operations - Batch operations instead of loops
9. Style & Formatting (LOW)
style-indentation - Use consistent indentation
style-file-structure - Follow consistent file structure
style-comments - Write useful comments
How to Use
Read individual reference files for detailed explanations and code examples:
Section definitions - Category structure and impact levels
Rule template - Template for adding new rules
Reference Files
File
Description
AGENTS.md
Complete compiled guide with all rules
references/_sections.md
Category definitions and ordering
assets/templates/_template.md
Template for new rules
metadata.json
Version and reference information
Key Sources
1 --- 2 name: shell 3 description: Shell Scripts Best Practices (Community) 4 --- 5 # Shell Scripts Best Practices (Community) 6 7 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. 8 9 ## When to Apply 10 11 Reference these guidelines when: 12 - Writing new bash or POSIX shell scripts 13 - Reviewing shell scripts for security vulnerabilities 14 - Debugging scripts that fail silently or behave unexpectedly 15 - Porting scripts between Linux, macOS, and containers 16 - Optimizing shell script performance 17 - Setting up CI/CD pipelines with shell scripts 18 19 ## Rule Categories by Priority 20 21 | Priority | Category | Impact | Prefix | Rules | 22 |----------|----------|--------|--------|-------| 23 | 1 | Safety & Security | CRITICAL | `safety-` | 6 | 24 | 2 | Portability | CRITICAL | `port-` | 5 | 25 | 3 | Error Handling | HIGH | `err-` | 8 | 26 | 4 | Variables & Data | HIGH | `var-` | 5 | 27 | 5 | Quoting & Expansion | MEDIUM-HIGH | `quote-` | 6 | 28 | 6 | Functions & Structure | MEDIUM | `func-` | 5 | 29 | 7 | Testing & Conditionals | MEDIUM | `test-` | 5 | 30 | 8 | Performance | LOW-MEDIUM | `perf-` | 6 | 31 | 9 | Style & Formatting | LOW | `style-` | 3 | 32 33 ## Quick Reference 34 35 ### 1. Safety & Security (CRITICAL) 36 37 - [`safety-command-injection`](references/safety-command-injection.md) - Prevent command injection from user input 38 - [`safety-eval-avoidance`](references/safety-eval-avoidance.md) - Avoid eval for dynamic commands 39 - [`safety-absolute-paths`](references/safety-absolute-paths.md) - Use absolute paths for external commands 40 - [`safety-temp-files`](references/safety-temp-files.md) - Create secure temporary files 41 - [`safety-suid-forbidden`](references/safety-suid-forbidden.md) - Never use SUID/SGID on shell scripts 42 - [`safety-argument-injection`](references/safety-argument-injection.md) - Prevent argument injection with double dash 43 44 ### 2. Portability (CRITICAL) 45 46 - [`port-shebang-selection`](references/port-shebang-selection.md) - Choose shebang based on portability needs 47 - [`port-avoid-bashisms`](references/port-avoid-bashisms.md) - Avoid bashisms in POSIX scripts 48 - [`port-printf-over-echo`](references/port-printf-over-echo.md) - Use printf instead of echo for portability 49 - [`port-export-syntax`](references/port-export-syntax.md) - Use portable export syntax 50 - [`port-test-portability`](references/port-test-portability.md) - Use portable test constructs 51 52 ### 3. Error Handling (HIGH) 53 54 - [`err-strict-mode`](references/err-strict-mode.md) - Use strict mode for error detection 55 - [`err-exit-codes`](references/err-exit-codes.md) - Use meaningful exit codes 56 - [`err-trap-cleanup`](references/err-trap-cleanup.md) - Use trap for cleanup on exit 57 - [`err-stderr-messages`](references/err-stderr-messages.md) - Send error messages to stderr 58 - [`err-pipefail`](references/err-pipefail.md) - Use pipefail to catch pipeline errors 59 - [`err-check-commands`](references/err-check-commands.md) - Check command success explicitly 60 - [`err-shellcheck`](references/err-shellcheck.md) - Use ShellCheck for static analysis 61 - [`err-debug-tracing`](references/err-debug-tracing.md) - Use debug tracing with set -x and PS4 62 63 ### 4. Variables & Data (HIGH) 64 65 - [`var-use-arrays`](references/var-use-arrays.md) - Use arrays for lists instead of strings 66 - [`var-local-scope`](references/var-local-scope.md) - Use local for function variables 67 - [`var-naming-conventions`](references/var-naming-conventions.md) - Follow variable naming conventions 68 - [`var-readonly-constants`](references/var-readonly-constants.md) - Use readonly for constants 69 - [`var-default-values`](references/var-default-values.md) - Use parameter expansion for defaults 70 71 ### 5. Quoting & Expansion (MEDIUM-HIGH) 72 73 - [`quote-always-quote-variables`](references/quote-always-quote-variables.md) - Always quote variable expansions 74 - [`quote-dollar-at`](references/quote-dollar-at.md) - Use "$@" for argument passing 75 - [`quote-command-substitution`](references/quote-command-substitution.md) - Quote command substitutions 76 - [`quote-brace-expansion`](references/quote-brace-expansion.md) - Use braces for variable clarity 77 - [`quote-here-documents`](references/quote-here-documents.md) - Use here documents for multi-line strings 78 - [`quote-glob-safety`](references/quote-glob-safety.md) - Control glob expansion explicitly 79 80 ### 6. Functions & Structure (MEDIUM) 81 82 - [`func-main-pattern`](references/func-main-pattern.md) - Use main() function pattern 83 - [`func-single-purpose`](references/func-single-purpose.md) - Write single-purpose functions 84 - [`func-return-values`](references/func-return-values.md) - Use return values correctly 85 - [`func-documentation`](references/func-documentation.md) - Document functions with header comments 86 - [`func-avoid-aliases`](references/func-avoid-aliases.md) - Prefer functions over aliases 87 88 ### 7. Testing & Conditionals (MEDIUM) 89 90 - [`test-double-brackets`](references/test-double-brackets.md) - Use [[ ]] for tests in bash 91 - [`test-arithmetic`](references/test-arithmetic.md) - Use (( )) for arithmetic comparisons 92 - [`test-explicit-empty`](references/test-explicit-empty.md) - Use explicit empty/non-empty string tests 93 - [`test-file-operators`](references/test-file-operators.md) - Use correct file test operators 94 - [`test-case-patterns`](references/test-case-patterns.md) - Use case for pattern matching 95 96 ### 8. Performance (LOW-MEDIUM) 97 98 - [`perf-builtins-over-external`](references/perf-builtins-over-external.md) - Use builtins over external commands 99 - [`perf-avoid-subshells`](references/perf-avoid-subshells.md) - Avoid unnecessary subshells 100 - [`perf-process-substitution`](references/perf-process-substitution.md) - Use process substitution for temp files 101 - [`perf-read-files`](references/perf-read-files.md) - Read files efficiently 102 - [`perf-parameter-expansion`](references/perf-parameter-expansion.md) - Use parameter expansion for string operations 103 - [`perf-batch-operations`](references/perf-batch-operations.md) - Batch operations instead of loops 104 105 ### 9. Style & Formatting (LOW) 106 107 - [`style-indentation`](references/style-indentation.md) - Use consistent indentation 108 - [`style-file-structure`](references/style-file-structure.md) - Follow consistent file structure 109 - [`style-comments`](references/style-comments.md) - Write useful comments 110 111 112 ## How to Use 113 114 Read individual reference files for detailed explanations and code examples: 115 116 - [Section definitions](references/_sections.md) - Category structure and impact levels 117 - [Rule template](assets/templates/_template.md) - Template for adding new rules 118 119 ## Reference Files 120 121 | File | Description | 122 |------|-------------| 123 | [AGENTS.md](AGENTS.md) | Complete compiled guide with all rules | 124 | [references/_sections.md](references/_sections.md) | Category definitions and ordering | 125 | [assets/templates/_template.md](assets/templates/_template.md) | Template for new rules | 126 | [metadata.json](metadata.json) | Version and reference information | 127 128 ## Key Sources 129 130 - [Google Shell Style Guide](https://google.github.io/styleguide/shellguide.html) 131 - [ShellCheck](https://www.shellcheck.net/) 132 - [Greg's Wiki (wooledge.org)](https://mywiki.wooledge.org/) 133 - [POSIX Shell Specification](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html)
ComeOnOliver/skillshub/tree/main/skills/pproenca/dot-skills/shell commit 38835ae909
Frequently asked questions How do I install the Shell skill? Run npx skillmds@latest add comeonoliver/shell in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the Shell skill do? Shell Scripts Best Practices (Community) It is listed under Coding & Dev Tools on SkillMD.
Is Shell safe to use? This skill has not completed SkillMD's automated safety review yet. Independent scanners report: SkillSpector: PASS, Skill Scanner: PASS. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with Shell? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is Shell free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published Shell? ComeOnOliver (@comeonoliver) published this skill. Their other Agent Skills are listed on their SkillMD profile.