关于 shell
面向 AI Agent 和 LLM 的 Shell 脚本实践指南,按影响优先级组织 49 条规则,并配有说明和正误示例,涵盖安全、可移植性与性能。
主要功能
- 01涵盖 9 类脚本主题
- 02提供 49 条优先级规则
- 03包含错误与正确示例
- 04涵盖安全性和可移植性
- 05提供错误处理与性能建议
适用场景
- 01编写 Bash 或 POSIX Shell 脚本
- 02审查脚本安全风险
- 03排查脚本静默失败
- 04将脚本移植至不同环境
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ pproenca/ dot-skills --skill shell安装后在 Claude Code 中输入/shell确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
AGENTS.mdmetadata.jsonREADME.mdassets/templates/_template.mdreferences/_sections.mderr-check-commands.mderr-debug-tracing.mderr-exit-codes.mderr-pipefail.mderr-shellcheck.mderr-stderr-messages.mderr-strict-mode.mderr-trap-cleanup.mdfunc-avoid-aliases.mdfunc-documentation.mdfunc-main-pattern.mdfunc-return-values.mdfunc-single-purpose.mdperf-avoid-subshells.mdperf-batch-operations.mdperf-builtins-over-external.mdperf-parameter-expansion.mdperf-process-substitution.mdperf-read-files.mdport-avoid-bashisms.mdport-export-syntax.mdport-printf-over-echo.mdport-shebang-selection.mdport-test-portability.mdquote-always-quote-variables.md详细介绍
Shell 脚本最佳实践(社区)
面向 AI Agent 和大模型的 Shell 脚本最佳实践综合指南。包含 9 个类别中的 49 条规则,按影响程度排序,从关键(安全性、可移植性)到渐进改进(风格)。每条规则都包含详细说明、对比错误与正确实现的真实示例,以及具体的影响指标。
适用场景
遇到以下情况时,请参考这些指南:
- 编写新的 bash 或 POSIX shell 脚本
- 检查 shell 脚本中的安全漏洞
- 调试静默失败或行为异常的脚本
- 在 Linux、macOS 和容器之间移植脚本
- 优化 shell 脚本性能
- 使用 shell 脚本设置 CI/CD 流水线
按优先级划分的规则类别
| 优先级 | 类别 | 影响 | 前缀 | 规则数 |
|---|---|---|---|---|
| 1 | 安全与防护 | 严重 | safety- | 6 |
| 2 | 可移植性 | 严重 | port- | 5 |
| 3 | 错误处理 | 高 | err- | 8 |
| 4 | 变量与数据 | 高 | var- | 5 |
| 5 | 引号与展开 | 中高 | quote- | 6 |
| 6 | 函数与结构 | 中 | func- | 5 |
| 7 | 测试与条件语句 | 中 | test- | 5 |
| 8 | 性能 | 低至中 | perf- | 6 |
| 9 | 风格与格式 | 低 | style- | 3 |
快速参考
1. 安全与防护(严重)
safety-command-injection- 防止用户输入导致命令注入safety-eval-avoidance- 避免使用 eval 执行动态命令safety-absolute-paths- 对外部命令使用绝对路径safety-temp-files- 创建安全的临时文件safety-suid-forbidden- 切勿在 shell 脚本上使用 SUID/SGIDsafety-argument-injection- 使用双短横线防止参数注入
2. 可移植性(严重)
port-shebang-selection- 根据可移植性需求选择 shebangport-avoid-bashisms- 在 POSIX 脚本中避免使用 bashismport-printf-over-echo- 为确保可移植性,使用 printf 代替 echoport-export-syntax- 使用可移植的 export 语法port-test-portability- 使用可移植的 test 结构
3. 错误处理(高)
err-strict-mode- 使用严格模式检测错误err-exit-codes- 使用有意义的退出码err-trap-cleanup- 使用 trap 在退出时执行清理err-stderr-messages- 将错误消息发送到 stderrerr-pipefail- 使用 pipefail 捕获管道错误err-check-commands- 显式检查命令是否成功err-shellcheck- 使用 ShellCheck 进行静态分析err-debug-tracing- 使用 set -x 和 PS4 进行调试跟踪
4. 变量与数据(高)
var-use-arrays- 使用数组而非字符串存储列表var-local-scope- 使用 local 声明函数变量var-naming-conventions- 遵循变量命名约定var-readonly-constants- 使用 readonly 声明常量var-default-values- 使用参数展开设置默认值
5. 引用与展开(中高)
quote-always-quote-variables- 始终为变量展开加引号quote-dollar-at- 使用 "$@" 传递参数quote-command-substitution- 为命令替换加引号quote-brace-expansion- 使用花括号让变量更清晰quote-here-documents- 使用 here 文档表示多行字符串quote-glob-safety- 明确控制 glob 展开
6. 函数与结构(中)
func-main-pattern- 使用 main() 函数模式func-single-purpose- 编写单一用途的函数func-return-values- 正确使用返回值func-documentation- 使用头部注释为函数编写文档func-avoid-aliases- 优先使用函数而非别名
7. 测试与条件判断(中)
test-double-brackets- 在 bash 测试中使用 [[ ]]test-arithmetic- 使用 (( )) 进行算术比较test-explicit-empty- 明确测试字符串是否为空test-file-operators- 使用正确的文件测试运算符test-case-patterns- 使用 case 进行模式匹配
8. 性能(低至中)
perf-builtins-over-external- 优先使用内置命令而非外部命令perf-avoid-subshells- 避免不必要的子 shellperf-process-substitution- 使用进程替换处理临时文件perf-read-files- 高效读取文件perf-parameter-expansion- 使用参数展开进行字符串操作perf-batch-operations- 批量执行操作,而非使用循环
9. 风格与格式(低)
style-indentation- 使用一致的缩进style-file-structure- 遵循一致的文件结构style-comments- 编写有用的注释
使用方法
阅读各个参考文件,了解详细说明和代码示例:
参考文件
| 文件 | 说明 |
|---|---|
| AGENTS.md | 包含所有规则的完整汇编指南 |
| references/_sections.md | 类别定义与排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |
主要来源
评论
支持 Markdown