Community编程与开发github.com

Jamie-BitFlight/bash-development

当用户要求“编写bash脚本”、“创建shell脚本”、“实现bash函数”、“解析bash参数”、“处理bash错误”,或涉及bash开发、shell脚本编写、脚本模板、现代bash模式时,应使用此技能。

bash-development 是什么?

此技能专注于Bash 5.1+脚本开发的核心模式和最佳实践。它提供了现代Bash惯用语法、错误处理、参数解析以及纯Bash替代外部命令的方法。通过使用`set -euo pipefail`、基于trap的错误处理和标准的参数解析模板,帮助开发者构建健壮、可维护的脚本。技能还涵盖了字符串操作、数组操作、文件操作、条件表达式等高效纯Bash实现。支持在Claude Code、Cursor、Codex等AI Agent平台上使用,通过参考文件(如完整的脚本模板、可复用的工具函数和纯Bash圣经文档)提供详细的代码示例和上下文感知的审查指南。使用此技能可以显著提升bash脚本的质量、性能和可移植性。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Jamie-BitFlight/claude_skills/tree/main/plugins/bash-development/skills/bash-development

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Bash 开发

Bash 5.1+ 脚本开发的核心模式与最佳实践。提供现代 Bash 惯用法、错误处理、参数解析以及纯 Bash 替代外部命令的方法。

脚本基础

每个脚本都从必要的头部开始:

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

set 选项说明:

  • -e - 命令失败时立即退出
  • -u - 将未设置的变量视为错误
  • -o pipefail - 如果管道中的任何命令失败,则管道失败

脚本元数据模式

SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}")
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
readonly SCRIPT_VERSION="1.0.0"
readonly SCRIPT_NAME SCRIPT_DIR

错误处理

为健壮的脚本实现基于 trap 的错误处理。

代码示例

参数解析

标准参数解析模板。

代码示例

变量最佳实践

始终使用花括号并引用变量:

# 正确
"${variable}"
"${array[@]}"

# 错误
$variable
${array[*]}  # 使用 [@] 进行正确迭代

对常量使用 readonly:

readonly CONFIG_FILE="/etc/app/config"
readonly -a VALID_OPTIONS=("opt1" "opt2" "opt3")

注意: 在源引(source)的脚本中切勿使用 readonly——在重新源引时会导致错误。

字符串操作(纯 Bash)

优先使用原生 Bash 参数扩展,而不是外部工具:

# 修剪空白
Trimmed="${string#"${string%%[![:space:]]*}"}"
trimmed="${trimmed%"${trimmed##*[![:space:]]}"}"

# 小写/大写(Bash 4+)
lower="${string,,}"
upper="${string^^}"

# 子字符串提取
substring="${string:0:10}"      # 前10个字符
suffix="${string: -5}"          # 最后5个字符

# 替换模式
replaced="${string//old/new}"   # 替换所有
replaced="${string/old/new}"    # 替换第一个

# 去除前缀/后缀
no_prefix="${string#prefix}"    # 最短匹配
no_prefix="${string##*/}"       # 最长匹配(basename)
no_suffix="${string%suffix}"    # 最短匹配
no_suffix="${string%%/*}"       # 最长匹配

数组操作

# 声明
declare -a indexed_array=()
declare -A assoc_array=()

# 安全迭代,使用 nullglob
shopt -s nullglob
for file in *.txt; do
    process "${file}"
done
shopt -u nullglob

# 数组长度
length="${#array[@]}"

# 追加元素
array+=("new_element")

# 带索引迭代
for i in "${!array[@]}"; do
    printf '%d: %s\n' "${i}" "${array[i]}"
done

文件操作

# 读取文件到字符串
content="$(<"${file}")"

# 读取文件到数组(Bash 4+)
mapfile -t lines < "${file}"

# 检查文件条件
[[ -f "${file}" ]]    # 常规文件存在
[[ -d "${dir}" ]]     # 目录存在
[[ -r "${file}" ]]    # 可读
[[ -w "${file}" ]]    # 可写
[[ -x "${file}" ]]    # 可执行
[[ -s "${file}" ]]    # 非空

# 安全创建临时文件
temp_file=$(mktemp)
trap 'rm -f "${temp_file}"' EXIT

条件表达式

使用 [[ ]] 作为条件判断(bash 特有,更强大):

# 字符串比较
[[ "${var}" == "value" ]]       # 相等
[[ "${var}" == pattern* ]]     # 通配符匹配
[[ "${var}" =~ ^regex$ ]]      # 正则匹配

# 数值比较
(( num > 10 ))                  # 算术比较
[[ "${num}" -gt 10 ]]          # 传统语法

# 复合条件
[[ -f "${file}" && -r "${file}" ]]
[[ "${opt}" == "a" || "${opt}" == "b" ]]

工具函数

代码示例

性能指南

  • 尽可能使用内置命令而非外部命令
  • 对大型数据集进行批量操作而非循环
  • 使用 printf 而非 echo,以获得可移植性和控制
  • 避免在紧凑循环中使用不必要的子 shell
  • 使用 [[ ]] 而非 [ ] 进行字符串比较

其他资源

参考文件

有关详细模式和示例,请参阅:

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

Jamie-BitFlight/agentskill-kaizen-meta-docs

Agentskill kaizen plugin documentation index. Load when needing to read about cross-platform notes, improvement plans, or DuckDB integration.

Jamie-BitFlight/bash-51-features

Bash 5.1 release features and improvements with practical examples. Use when working with Bash 5.1 features, epoch time variables, redirection enhancements, or when user asks about Bash 5.1 changes, new features, or version-specific capabilities.

Jamie-BitFlight/bash-52-features

Bash 5.2 release features and improvements with practical examples. Use when working with Bash 5.2 features, variable handling enhancements, readline improvements, or when user asks about Bash 5.2 changes, new features, or version-specific capabilities.

Jamie-BitFlight/bash-53-features

Bash 5.3 release features and improvements with practical examples. Use when working with Bash 5.3 features, new command substitution, GLOBSORT, loadable builtins, or when user asks about Bash 5.3 changes, new features, or version-specific capabilities.

Jamie-BitFlight/bash-lint

This skill should be used when the user asks to "lint bash script", "run shellcheck", "format shell script", "use shfmt", "fix shellcheck errors", or mentions shell script linting, formatting, code quality, or pre-commit hooks for bash.

Jamie-BitFlight/bash-logging

This skill should be used when the user asks to "add logging to bash script", "colorize output", "implement log levels", "CI/CD sections", "terminal colors in bash", or mentions logging functions, emoji output, collapsible CI sections, or shlocksmith.

Jamie-BitFlight/bash-portability

This skill should be used when the user asks about "POSIX compatibility", "portable shell scripts", "cross-shell compatibility", "bashisms", "shebang selection", or mentions writing scripts that work on different shells (bash, sh, dash, zsh) or different systems.

Jamie-BitFlight/bash-testing

This skill should be used when the user asks to "test bash script", "write shell tests", "use shunit2", "use shellspec", "create test suite for bash", or mentions unit testing, test frameworks, mocking, or test-driven development for shell scripts.

Jamie-BitFlight/brainstorming-skill

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or when users request help with ideation, marketing, and strategic planning. Explores user intent, requirements, and design before implementation using research-validated prompt patterns.

Jamie-BitFlight/clang-format

Configure clang-format code formatting. Use when: user mentions clang-format or .clang-format, analyzing code style/patterns, creating/modifying formatting config, troubleshooting formatting, brace styles/indentation/spacing/alignment/pointer alignment, or codifying conventions.

Jamie-BitFlight/commitlint

When setting up commit message validation for a project. When project has commitlint.config.js or .commitlintrc files. When configuring CI/CD to enforce commit format. When extracting commit rules for LLM prompt generation. When debugging commit message rejection errors.

Jamie-BitFlight/conventional-commits

When writing a git commit message. When task completes and changes need committing. When project uses semantic-release, commitizen, git-cliff. When choosing between feat/fix/chore/docs types. When indicating breaking changes. When generating changelogs from commit history.

Jamie-BitFlight/dasel-reference

Use when querying, modifying, or converting JSON, YAML, TOML, XML, CSV, HCL, or INI with dasel v3. Complete reference for selectors, functions, conditionals, variables, spread operator, type casting, and format-specific patterns.

Jamie-BitFlight/data-exploration

Use when exploring unknown structured data files with dasel v3 — discover schema, list keys, find nested values, sample arrays, identify data types across JSON, YAML, TOML, XML, CSV, HCL, INI formats

Jamie-BitFlight/data-transformation

Use when modifying, converting, or transforming structured data with dasel v3 — in-place mutations, format conversion, batch operations, array manipulation, object construction, and merge patterns across JSON, YAML, TOML, XML, CSV, HCL, INI

Jamie-BitFlight/delegate

Decompose substantive work into phases, dispatch each phase to a sub-agent, and adjudicate what comes back. Use whenever a request asks for implementation, investigation, a fix, a review, or any change to files — including small ones — and whenever you are about to read source or run a diagnostic yourself instead of handing it off. Also use when a report from a sub-agent needs judging, when a phase needs re-dispatching, or when a user names one instance of a pattern. Does not apply when your own prompt begins "Your ROLE_TYPE is sub-agent." — then follow references/sub-agent-contract.md instead.

Jamie-BitFlight/enterprise-hibernate-hbm

Dasel v3 query patterns for Hibernate .hbm.xml mapping files — entity-table binding, Java property-to-column extraction, one-to-many set/list/bag relationship tracing, many-to-one foreign key discovery, batch scanning across 60+ HBM files. Use when querying Hibernate ORM class mappings, extracting schema metadata from Java persistence layer, or auditing entity-column relationships in enterprise legacy codebases.

Jamie-BitFlight/enterprise-installanywhere

Dasel v3 query patterns for InstallAnywhere .iap_xml installer definitions — use when querying action sequences, discovering variables, resolving platform conditions, navigating panels, or comparing installer variants. Files are 2.5+ MB, 65,000+ lines — too large for context reads, requires structural dasel queries.

Jamie-BitFlight/enterprise-maven-pom

Dasel v3 selector patterns for Maven POM XML files — use when querying dependency versions, filtering by groupId or scope, extracting module hierarchy from parent POMs, or detecting version conflicts across enterprise multi-module Java projects. Load this skill when working with pom.xml files using dasel.

Jamie-BitFlight/enterprise-spring-xml

Dasel v3 selectors for Spring bean factory XML — use when querying any Spring ApplicationContext XML for bean discovery, dependency wiring, JMS destination mapping, property injection extraction, or cross-bean reference tracing. Load this skill before writing dasel selectors against Spring bean XML files (applicationContext.xml, *_beans.xml, spring-*.xml).

相关技能