Shell

作者 pproencacf93c57cac89無授權條款215 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫7 週前更新

Shell scripting best practices for writing safe, portable, and maintainable bash/sh scripts. Use when writing, reviewing, or refactoring shell scripts, Dockerfile RUN commands, Makefile recipes, CI pipeline scripts, cron jobs, or systemd ExecStart directives. Triggers on bash, sh, POSIX, ShellCheck, error handling, quoting, variables, set -euo pipefail.

AI 產生的概覽

一份包含 49 條 shell 指令碼最佳實務的參考指南,用於撰寫安全、可攜且易於維護的 bash 與 sh 指令碼。

功能
提供依優先順序排列的 49 條規則,分為 9 個類別,涵蓋安全與防護、可攜性、錯誤處理、變數、引號、函式、測試、效能與風格。每條規則都有獨立的參考檔案,內含說明以及錯誤與正確寫法的對照範例。此外還提供完整彙編指南、章節定義以及新增規則的範本。
適用情境
適用於撰寫新的 bash 或 POSIX shell 指令碼、審查指令碼的安全漏洞、偵錯無聲失敗或行為異常的指令碼、在 Linux、macOS 與容器之間移植指令碼、最佳化指令碼效能,以及建置 CI/CD 管線指令碼時。
執行需求
不需要指令碼或執行階段相依性,僅為純說明性參考資料;需要閱讀隨附的 Markdown 參考檔案。

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

PriorityCategoryImpactPrefixRules
1Safety & SecurityCRITICALsafety-6
2PortabilityCRITICALport-5
3Error HandlingHIGHerr-8
4Variables & DataHIGHvar-5
5Quoting & ExpansionMEDIUM-HIGHquote-6
6Functions & StructureMEDIUMfunc-5
7Testing & ConditionalsMEDIUMtest-5
8PerformanceLOW-MEDIUMperf-6
9Style & FormattingLOWstyle-3

Quick Reference

1. Safety & Security (CRITICAL)

  • safety-command-injection [blocked] - Prevent command injection from user input
  • safety-eval-avoidance [blocked] - Avoid eval for dynamic commands
  • safety-absolute-paths [blocked] - Use absolute paths for external commands
  • safety-temp-files [blocked] - Create secure temporary files
  • safety-suid-forbidden [blocked] - Never use SUID/SGID on shell scripts
  • safety-argument-injection [blocked] - Prevent argument injection with double dash

2. Portability (CRITICAL)

  • port-shebang-selection [blocked] - Choose shebang based on portability needs
  • port-avoid-bashisms [blocked] - Avoid bashisms in POSIX scripts
  • port-printf-over-echo [blocked] - Use printf instead of echo for portability
  • port-export-syntax [blocked] - Use portable export syntax
  • port-test-portability [blocked] - Use portable test constructs

3. Error Handling (HIGH)

  • err-strict-mode [blocked] - Use strict mode for error detection
  • err-exit-codes [blocked] - Use meaningful exit codes
  • err-trap-cleanup [blocked] - Use trap for cleanup on exit
  • err-stderr-messages [blocked] - Send error messages to stderr
  • err-pipefail [blocked] - Use pipefail to catch pipeline errors
  • err-check-commands [blocked] - Check command success explicitly
  • err-shellcheck [blocked] - Use ShellCheck for static analysis
  • err-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 strings
  • var-local-scope [blocked] - Use local for function variables
  • var-naming-conventions [blocked] - Follow variable naming conventions
  • var-readonly-constants [blocked] - Use readonly for constants
  • var-default-values [blocked] - Use parameter expansion for defaults

5. Quoting & Expansion (MEDIUM-HIGH)

  • quote-always-quote-variables [blocked] - Always quote variable expansions
  • quote-dollar-at [blocked] - Use "$@" for argument passing
  • quote-command-substitution [blocked] - Quote command substitutions
  • quote-brace-expansion [blocked] - Use braces for variable clarity
  • quote-here-documents [blocked] - Use here documents for multi-line strings
  • quote-glob-safety [blocked] - Control glob expansion explicitly

6. Functions & Structure (MEDIUM)

  • func-main-pattern [blocked] - Use main() function pattern
  • func-single-purpose [blocked] - Write single-purpose functions
  • func-return-values [blocked] - Use return values correctly
  • func-documentation [blocked] - Document functions with header comments
  • func-avoid-aliases [blocked] - Prefer functions over aliases

7. Testing & Conditionals (MEDIUM)

  • test-double-brackets [blocked] - Use [[ ]] for tests in bash
  • test-arithmetic [blocked] - Use (( )) for arithmetic comparisons
  • test-explicit-empty [blocked] - Use explicit empty/non-empty string tests
  • test-file-operators [blocked] - Use correct file test operators
  • test-case-patterns [blocked] - Use case for pattern matching

8. Performance (LOW-MEDIUM)

  • perf-builtins-over-external [blocked] - Use builtins over external commands
  • perf-avoid-subshells [blocked] - Avoid unnecessary subshells
  • perf-process-substitution [blocked] - Use process substitution for temp files
  • perf-read-files [blocked] - Read files efficiently
  • perf-parameter-expansion [blocked] - Use parameter expansion for string operations
  • perf-batch-operations [blocked] - Batch operations instead of loops

9. Style & Formatting (LOW)

  • style-indentation [blocked] - Use consistent indentation
  • style-file-structure [blocked] - Follow consistent file structure
  • style-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

Reference Files

FileDescription
AGENTS.md [blocked]Complete compiled guide with all rules
references/_sections.md [blocked]Category definitions and ordering
assets/templates/_template.md [blocked]Template for new rules
metadata.json [blocked]Version and reference information

Key Sources

來源與署名

來源:pproenca/dot-skills位於skills/.experimental/shell提交cf93c57

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架