plugins/languages/bash/skills/posix/SKILL.md
POSIX sh portability guidance: when to choose /bin/sh vs /usr/bin/env bash, dash/ash vs bash feature matrix, macOS bash 3.2 legacy constraints, busybox/Alpine ash quirks, portable replacements for [[ ]] / arrays / process substitution, and shellcheck -s sh enforcement. Use when targeting Alpine containers, OpenWrt, BSD systems, init scripts, or any environment without bash. Triggers on "POSIX sh", "/bin/sh", "dash", "ash", "busybox shell", "macOS bash 3.2", "alpine 脚本", "可移植脚本".
npx skillsauth add lazygophers/ccplugin bash-posixInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
| 场景 | 推荐 |
|------|------|
| Alpine / busybox 容器 init | POSIX sh (#!/bin/sh) |
| OpenWrt / 路由器 / 嵌入式 | POSIX sh |
| Debian / Ubuntu 启动脚本(dash) | POSIX sh |
| macOS 用户脚本(系统 bash 3.2) | Bash 4+(明确依赖)或 POSIX sh |
| 现代 Linux 桌面 / 服务器 | Bash 5.2+ |
| 项目内工具脚本 | Bash 5.2+(明确版本) |
原则:默认 Bash,只在跨极简环境时降级 POSIX。降级前评估是否能换 brew/apk 装 bash。
| Bash | POSIX sh 替代 |
|------|--------------|
| [[ str == pat ]] | case "$str" in pat) ;; esac |
| [[ str =~ regex ]] | echo "$str" \| grep -Eq 'regex' |
| (( a > b )) | [ "$a" -gt "$b" ] |
| arr=(a b c) | 位置参数 set -- a b c |
| ${arr[@]} | "$@" |
| ${var,,} / ${var^^} | tr '[:upper:]' '[:lower:]' |
| local | 无;用函数命名空间或 subshell |
| $'...' | printf '%b' '...' |
| <(cmd) 进程替换 | 管道 / 临时文件 |
| mapfile -t a < f | while IFS= read -r line; do ...; done < f |
| read -p prompt | printf '%s' prompt; read var |
| echo -e | printf '%b\n' |
| ${var:-x} | 可用(POSIX) |
| ${var/from/to} | echo "$var" \| sed 's/from/to/' |
| 关联数组 declare -A | 多变量前缀或 awk |
macOS 因许可证未升级,系统 /bin/bash 仍是 3.2.57 (2007):
declare -A)${var,,} / ${var^^} 大小写[[ -v var ]](检查是否定义)mapfile / readarraywait -n${var@Q} 参数转换<(...) 有但 macOS sandbox 可能限制应对策略:
(( BASH_VERSINFO[0] >= 4 )) || { echo "need bash 4+" >&2; exit 1; }#!/usr/bin/env bash 由 PATH(brew install bash → /opt/homebrew/bin/bash)解析。# 检查 bash 主版本
need_bash() {
if [[ -z "${BASH_VERSION:-}" ]]; then
echo "error: this script requires bash, not sh" >&2
exit 1
fi
local major="${BASH_VERSINFO[0]}"
if (( major < 4 )); then
echo "error: bash >= 4 required (have ${BASH_VERSION})" >&2
exit 1
fi
}
POSIX 检测自身:
# POSIX 不能用 BASH_VERSION(dash 中未定义且 set -u 触发)
if [ -n "${BASH_VERSION:-}" ]; then
echo "running on bash"
fi
#!/bin/sh
# brief: portable script
set -eu
# pipefail 不可用;按需手动 wait
log() { printf '%s\n' "$*" >&2; }
die() { log "fatal: $*"; exit 1; }
# 位置参数代替数组
process_all() {
[ $# -gt 0 ] || die "no args"
for item in "$@"; do
printf 'item=%s\n' "$item"
done
}
# case 代替 [[ == ]]
case "${1:-}" in
*.txt) log "text" ;;
*.md) log "markdown" ;;
*) die "unknown ext" ;;
esac
# 算术
n=10
if [ "$n" -gt 5 ]; then
log "big"
fi
# 字符串小写
lower=$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')
process_all "$@"
shellcheck -s sh script.sh # 强制 POSIX
shellcheck -s bash script.sh # bash 模式
shellcheck -s dash script.sh # dash 模式(Debian /bin/sh)
# 文件内声明(推荐)
# shellcheck shell=sh
local 在 ash 中可用(虽非 POSIX 标准)。echo -e 在 ash 中默认行为不同;统一 printf。command -v ? 实际上有,是 POSIX 标准;放心用。/bin/sh)shellcheck -s sh[[ ]] / 数组 / == / <() / (( )) 出现在 POSIX 脚本/bin/sh 而非 /bin/bashtools
UI/UX 与布局设计——做界面布局/结构/导航/组件/交互的设计决策。触发:做UI/UX/布局/排版/导航/组件/交互/栅格/响应式/图表选型/字体配对。按媒介路由 HTML/Web、原生 App(iOS/Android/桌面)、CLI、TUI。需后端动态系统不适用;配色/主题/色板走姊妹 skill design-color。
tools
主题与配色设计——做颜色搭配/调色板/主题/品牌色阶/暗模式的设计决策。触发:选配色/调色/主题/色板/品牌色/暗模式/对比度/色盲/UI风格。按媒介路由 HTML/Web(CSS变量)、原生App(平台token)、CLI(ANSI)、TUI(真彩/256/16降级)。保证可访问性(对比度/色盲安全)。需后端动态系统不适用;UI/UX 布局/组件/交互走姊妹 skill design-uiux。
tools
跨任意组件(plugin/skill/agent/command)的验证驱动优化循环纪律 skill。当用户要优化某个已有组件却无明确方向、或要防止改了反而更差(自评乐观偏差 / 多维同改归因失效 / 为凑分加废话膨胀)、或要把一套通用「评分→单变量改→改后验证严格更好才留否则回滚→触顶停」的纪律套到任意组件上时使用。管优化过程本身的纪律(validation gate / ratchet / 独立验证 / 触顶停),不评单组件深度(交 skill-dev),不查插件接线(交 plugin-dev)。仅手动 /optimize-any 触发。
data-ai
两层规则记忆 (基于 .skein/spec)。planning 时 recall 召回相关规则、task finish 后 sediment 沉淀学习 + prune 自动精简过期/重复/断链规则。core 常驻硬规 + recall 按需召回, 经判定门自动写盘 (不逐次问用户)。产出 .skein/spec 下 core/recall 规则文件 + index。另支持空仓 bootstrap 播种规则基线、记忆大面积失效 (大重构/换栈) 时 reconstruct 可逆归档后按项目类型分型重建、maintain 手动体检 (超预算/stale/断链/重复/废弃, --apply 自动修复)、auto-fix (Stop hook 写 .pending-fix 标记 → main 派 skein-specer bg 跑 maintain --apply 全自动修, 断链只报告)。