shell
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseShell Scripts Best Practices
Shell脚本最佳实践
Comprehensive best practices guide for shell scripting, designed for AI agents and LLMs. Contains 48 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.
这是一份面向AI Agent和大语言模型的Shell脚本综合最佳实践指南。包含9个类别下的48条规则,按影响优先级排序,从关键(安全、可移植性)到增量(风格)依次排列。每条规则都配有详细说明、对比错误与正确实现的真实示例,以及具体的影响指标。
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
在以下场景中可参考本指南:
- 编写新的bash或POSIX Shell脚本
- 审查Shell脚本以排查安全漏洞
- 调试静默失败或行为异常的脚本
- 在Linux、macOS和容器之间移植脚本
- 优化Shell脚本的性能
- 使用Shell脚本搭建CI/CD流水线
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix | Rules |
|---|---|---|---|---|
| 1 | Safety & Security | CRITICAL | | 6 |
| 2 | Portability | CRITICAL | | 5 |
| 3 | Error Handling | HIGH | | 6 |
| 4 | Variables & Data | HIGH | | 5 |
| 5 | Quoting & Expansion | MEDIUM-HIGH | | 6 |
| 6 | Functions & Structure | MEDIUM | | 5 |
| 7 | Testing & Conditionals | MEDIUM | | 5 |
| 8 | Performance | LOW-MEDIUM | | 6 |
| 9 | Style & Formatting | LOW | | 4 |
| 优先级 | 类别 | 影响等级 | 前缀 | 规则数量 |
|---|---|---|---|---|
| 1 | 安全与防护 | 关键 | | 6 |
| 2 | 可移植性 | 关键 | | 5 |
| 3 | 错误处理 | 高 | | 6 |
| 4 | 变量与数据 | 高 | | 5 |
| 5 | 引用与展开 | 中高 | | 6 |
| 6 | 函数与结构 | 中 | | 5 |
| 7 | 测试与条件判断 | 中 | | 5 |
| 8 | 性能 | 中低 | | 6 |
| 9 | 风格与格式 | 低 | | 4 |
Quick Reference
快速参考
1. Safety & Security (CRITICAL)
1. 安全与防护(关键)
- - Prevent command injection from user input
safety-command-injection - - Avoid eval for dynamic commands
safety-eval-avoidance - - Use absolute paths for external commands
safety-absolute-paths - - Create secure temporary files
safety-temp-files - - Never use SUID/SGID on shell scripts
safety-suid-forbidden - - Prevent argument injection with double dash
safety-argument-injection
- - 防止用户输入导致的命令注入
safety-command-injection - - 避免使用eval执行动态命令
safety-eval-avoidance - - 外部命令使用绝对路径
safety-absolute-paths - - 创建安全的临时文件
safety-temp-files - - 绝不要在Shell脚本上使用SUID/SGID权限
safety-suid-forbidden - - 使用双破折号防止参数注入
safety-argument-injection
2. Portability (CRITICAL)
2. 可移植性(关键)
- - Choose shebang based on portability needs
port-shebang-selection - - Avoid bashisms in POSIX scripts
port-avoid-bashisms - - Use printf instead of echo for portability
port-printf-over-echo - - Use portable export syntax
port-export-syntax - - Use portable test constructs
port-test-portability
- - 根据可移植性需求选择Shebang
port-shebang-selection - - 在POSIX脚本中避免使用Bash特有语法
port-avoid-bashisms - - 为了可移植性使用printf而非echo
port-printf-over-echo - - 使用可移植的export语法
port-export-syntax - - 使用可移植的测试结构
port-test-portability
3. Error Handling (HIGH)
3. 错误处理(高)
- - Use strict mode for error detection
err-strict-mode - - Use meaningful exit codes
err-exit-codes - - Use trap for cleanup on exit
err-trap-cleanup - - Send error messages to stderr
err-stderr-messages - - Use pipefail to catch pipeline errors
err-pipefail - - Check command success explicitly
err-check-commands
- - 使用严格模式检测错误
err-strict-mode - - 使用有意义的退出码
err-exit-codes - - 使用trap在退出时执行清理操作
err-trap-cleanup - - 将错误消息发送至stderr
err-stderr-messages - - 使用pipefail捕获流水线错误
err-pipefail - - 显式检查命令执行是否成功
err-check-commands
4. Variables & Data (HIGH)
4. 变量与数据(高)
- - Use arrays for lists instead of strings
var-use-arrays - - Use local for function variables
var-local-scope - - Follow variable naming conventions
var-naming-conventions - - Use readonly for constants
var-readonly-constants - - Use parameter expansion for defaults
var-default-values
- - 使用数组而非字符串存储列表
var-use-arrays - - 函数变量使用local声明作用域
var-local-scope - - 遵循变量命名规范
var-naming-conventions - - 使用readonly声明常量
var-readonly-constants - - 使用参数展开设置默认值
var-default-values
5. Quoting & Expansion (MEDIUM-HIGH)
5. 引用与展开(中高)
- - Always quote variable expansions
quote-always-quote-variables - - Use "$@" for argument passing
quote-dollar-at - - Quote command substitutions
quote-command-substitution - - Use braces for variable clarity
quote-brace-expansion - - Use here documents for multi-line strings
quote-here-documents - - Control glob expansion explicitly
quote-glob-safety
- - 始终对变量展开进行引用
quote-always-quote-variables - - 使用"$@"传递参数
quote-dollar-at - - 对命令替换进行引用
quote-command-substitution - - 使用大括号提高变量可读性
quote-brace-expansion - - 使用Here Document处理多行字符串
quote-here-documents - - 显式控制通配符展开
quote-glob-safety
6. Functions & Structure (MEDIUM)
6. 函数与结构(中)
- - Use main() function pattern
func-main-pattern - - Write single-purpose functions
func-single-purpose - - Use return values correctly
func-return-values - - Document functions with header comments
func-documentation - - Prefer functions over aliases
func-avoid-aliases
- - 使用main()函数模式
func-main-pattern - - 编写单一职责的函数
func-single-purpose - - 正确使用返回值
func-return-values - - 使用头部注释为函数添加文档
func-documentation - - 优先使用函数而非别名
func-avoid-aliases
7. Testing & Conditionals (MEDIUM)
7. 测试与条件判断(中)
- - Use [[ ]] for tests in bash
test-double-brackets - - Use (( )) for arithmetic comparisons
test-arithmetic - - Use explicit empty/non-empty string tests
test-explicit-empty - - Use correct file test operators
test-file-operators - - Use case for pattern matching
test-case-patterns
- - 在Bash中使用[[ ]]进行测试
test-double-brackets - - 使用(( ))进行算术比较
test-arithmetic - - 使用显式的空/非空字符串测试
test-explicit-empty - - 使用正确的文件测试操作符
test-file-operators - - 使用case进行模式匹配
test-case-patterns
8. Performance (LOW-MEDIUM)
8. 性能(中低)
- - Use builtins over external commands
perf-builtins-over-external - - Avoid unnecessary subshells
perf-avoid-subshells - - Use process substitution for temp files
perf-process-substitution - - Read files efficiently
perf-read-files - - Use parameter expansion for string operations
perf-parameter-expansion - - Batch operations instead of loops
perf-batch-operations
- - 优先使用内置命令而非外部命令
perf-builtins-over-external - - 避免不必要的子Shell
perf-avoid-subshells - - 使用进程替换替代临时文件
perf-process-substitution - - 高效读取文件
perf-read-files - - 使用参数展开进行字符串操作
perf-parameter-expansion - - 批量操作而非循环处理
perf-batch-operations
9. Style & Formatting (LOW)
9. 风格与格式(低)
- - Use consistent indentation
style-indentation - - Follow consistent file structure
style-file-structure - - Write useful comments
style-comments - - Use ShellCheck for static analysis
style-shellcheck
- - 使用一致的缩进
style-indentation - - 遵循一致的文件结构
style-file-structure - - 编写有用的注释
style-comments - - 使用ShellCheck进行静态代码分析
style-shellcheck
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
阅读单个参考文件以获取详细说明和代码示例:
- Section definitions - 类别结构与影响等级说明
- Rule template - 添加新规则的模板
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 |
| 文件 | 描述 |
|---|---|
| AGENTS.md | 包含所有规则的完整编译指南 |
| references/_sections.md | 类别定义与排序说明 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |