shell-scripting-safe

A safety guide for writing Bash scripts that expose errors and avoid accidental file damage.

In plain words
What is it for?
Use it when creating or modifying shell scripts, automating operational procedures, or handling files and directories in potentially destructive steps.
Why use it?
It reduces silent failures and protects operations involving paths, deletion, directory replacement, and remote commands by enforcing strict error handling and safe variable use.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/wade-devcode/awesome-coding-skills-cn/shell-scripting-safe
Any agent
npx skills add Wade-DevCode/awesome-coding-skills-cn --skill shell-scripting-safe
Clone the repo
git clone --depth 1 https://github.com/Wade-DevCode/awesome-coding-skills-cn

Made for: Claude Code, Codex.

Per session 24 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,490 The whole file, excluding the scripts and references it only reads on demand.
Security scan C 2 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00024 $0.02490
Opus 5 $0.00012 $0.01245
Sonnet 5 $0.00005 $0.00498
Haiku 4.5 $0.00002 $0.00249

Measured 2d ago against content hash 7ac3e082630b, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade C, and why

shell-scripting-safe scanned grade C with 2 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Recursive force deletehighDestructive command

rm -rf with a variable or a broad path is one typo away from removing the wrong tree.

**为什么:** AI 生成的 bash 脚本默认不加这三个选项,导致静默失败危害极大。曾见真实事故:`TARGET_DIR=""` 变量赋值失败(来自上一条命令出错),下一步 `rm -rf "$TARGET_DIR/"` 展开为 `rm -rf "/"` 并成功执行——因为没有 `-u`,空变量不报错;因为没有 `-e`,上一步出错没停下来。加上这三行,相同场景会在变量赋值处立即报错退出。

Makes network callslowCapability

Not a fault in itself. Listed so you know the mod talks to something, and to what.

for cmd in jq aws curl; do
skills/shell-scripting-safe/SKILL.md · 214 lines

How it starts

The opening of the file, as written. The whole thing — 214 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Shell 脚本安全

何时用

  • 新写或修改任何 .sh / bash 脚本。
  • 脚本涉及文件删除、目录覆盖、远程操作等危险步骤时。
  • 发现脚本某步骤出错后默默继续、没有任何输出时。
  • 把手动运维步骤固化成自动化脚本前做方案设计。

核心规则

1. 头部加 set -euo pipefail,让错误立即暴露

规则: 每个脚本第一行(shebang 之后)加 set -euo pipefail,确保命令非零退出立即终止脚本(-e),引用未定义变量报错(-u),管道中间命令失败也能被捕获(-o pipefail)。

为什么: AI 生成的 bash 脚本默认不加这三个选项,导致静默失败危害极大。曾见真实事故:TARGET_DIR="" 变量赋值失败(来自上一条命令出错),下一步 rm -rf "$TARGET_DIR/" 展开为 rm -rf "/" 并成功执行——因为没有 -u,空变量不报错;因为没有 -e,上一步出错没停下来。加上这三行,相同场景会在变量赋值处立即报错退出。

怎么做:

#!/usr/bin/env bash
set -euo pipefail

# 之后的所有命令:任何一步失败即停止,未定义变量即报错
  • 若某条命令允许失败,用 command || truecommand || echo "可选步骤失败,继续" 显式豁免,不要关掉全局 -e
  • 子 shell 调用的脚本同样需要各自设置,不继承父脚本的 set 选项。

2. 变量永远加双引号

规则: 引用任何变量时都用双引号:"$var""$@""${array[@]}";只在明确需要分词或通配符展开时才省略引号。

为什么: AI 写 bash 时很少给变量加引号,遇到含空格或通配符的路径时立刻出事。典型案例:cp $SRC $DSTSRC="/home/user/my files/data.txt" 时被 shell 解析为 cp /home/user/my files/data.txt $DST,变成三个参数,cp 报错或拷错文件。更危险的是 rm -rf $DIR/*,若 DIR/tmp/app (带尾随空格),展开结果不可预料。

怎么做:

# 反例
cp $SRC $DST
rm -rf $DIR/*

# 正例
cp "$SRC" "$DST"
rm -rf "${DIR:?}/"*      # :? 额外保证变量非空,空则报错退出
  • 数组展开用 "${arr[@]}" 而非 ${arr[*]},保留每个元素的边界。
  • 命令替换也加引号:output="$(some_command)"

3. 危险操作前校验变量非空与路径合法,提供 dry-run

规则: 执行 rm -rfddmkfs、大范围覆盖等不可逆操作前,必须:① 用 ${VAR:?错误信息} 或显式 if [ -z "$VAR" ] 校验关键变量非空,② 检查路径符合预期(不是根目录、不是系统目录),③ 支持 DRY_RUN=1 模式只打印不执行。

为什么: AI 生成的清理脚本几乎从不做这类防御,使用者一旦环境变量配置错误,rm -rf "$DEPLOY_DIR/" 就会变成 rm -rf "/"rm -rf "/var/" 并实际执行。这类事故在运维历史上反复出现,恢复成本极高。

怎么做:

#!/usr/bin/env bash
set -euo pipefail

DEPLOY_DIR="${DEPLOY_DIR:?必须设置 DEPLOY_DIR 环境变量}"
DRY_RUN="${DRY_RUN:-0}"

# 路径安全检查
if [[ "$DEPLOY_DIR" == "/" || "$DEPLOY_DIR" == "/usr" || "$DEPLOY_DIR" == "/etc" ]]; then
  echo "❌ DEPLOY_DIR 疑似系统目录,拒绝执行" >&2
  exit 1
fi

do_rm() {
  if [[ "$DRY_RUN" == "1" ]]; then
    echo "[DRY_RUN] rm -rf $1"
  else
    rm -rf "$1"
  fi
}

do_rm "${DEPLOY_DIR}/old_release"

Read the full file on GitHub · 214 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 214 lines · 24 tokens per session scan C 7ac3e082630b

Subscribe to this mod's changes

shell-scripting-safe is a skill published in the GitHub repository Wade-DevCode/awesome-coding-skills-cn (6 stars, last pushed 2mo ago), licensed MIT. It adds 24 tokens to every session and 2,490 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it C with 2 findings (recursive force delete, makes network calls). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

Other skills, from other repositories

chinese-git-workflow

国内 Git 平台配置参考——Gitee、Coding.net、极狐 GitLab、CNB 的 SSH/HTTPS/凭据/CI 接入差异与镜像同步配置。仅在用户显式 /chinese-git-workflow 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 69 tokens

brainstorming

在任何创造性工作之前必须使用此技能——创建功能、构建组件、添加功能或修改行为。在实现之前先探索用户意图、需求和设计。.

jnMetaCode/superpowers-zh · 40 tokens

chinese-code-review

中文 review 沟通参考——话术模板、分级标注(必须修复/建议修改/仅供参考)、国内团队常见反模式应对。仅在用户显式 /chinese-code-review 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 62 tokens

chinese-commit-conventions

中文 commit 与 changelog 配置参考——Conventional Commits 中文适配、commitlint/husky/commitizen 中文模板、conventional-changelog 中文配置。仅在用户显式 /chinese-commit-conventions 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 65 tokens

chinese-documentation

中文文档排版参考——中英文空格、全半角标点、术语保留、链接格式、中文文案排版指北约定。仅在用户显式 /chinese-documentation 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 62 tokens

systematic-debugging

Skill "systematic-debugging" from jnMetaCode/superpowers-zh, covering 系统化调试, 概述, 铁律, 何时使用 and 四个阶段.

jnMetaCode/superpowers-zh · 24 tokens