Community编程与开发github.com

Jamie-BitFlight/bash-51-features

Bash 5.1 版本的新功能和改进,包含实用示例。适用于使用 Bash 5.1 新特性、epoch 时间变量、重定向增强,或者用户询问 Bash 5.1 变更、新特性或版本特定功能时。

bash-51-features 是什么?

该技能详细介绍了 Bash 5.1 版本(2020 年 12 月发布)引入的重要新特性和改进,并通过大量可运行的代码示例展示其实际应用。核心内容包括:新增的 `EPOCHSECONDS` 和 `EPOCHREALTIME` 变量,用于高精度时间戳和性能测量;`SRANDOM` 变量提供密码学强度的随机数;改进的 `{varname}` 重定向语法,支持命名文件描述符,提升代码可读性和可维护性;增强的关联数组处理;以及改进的信号处理和 Readline 集成。技能还提供了版本兼容性检查和最佳实践建议,帮助用户在保持向后兼容性的同时安全使用新特性。该技能适用于 Claude Code、Cursor、Codex 等 AI 编码助手平台,是编写高性能、可维护 Bash 脚本的实用参考。

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

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

在你喜欢的 AI 中提问

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

文档

Bash 5.1 功能与改进

Bash 5.1 于 2020 年 12 月发布,为现代 Shell 脚本编程引入了多项值得关注的功能和改进。

新的 Shell 变量

EPOCHSECONDS 和 EPOCHREALTIME

两个用于获取当前时间的新特殊变量:

# 获取当前 Unix 时间戳(秒)
echo "当前时间戳:${EPOCHSECONDS}"

# 获取微秒精度的高精度时间戳
echo "高精度时间戳:${EPOCHREALTIME}"

# 实用示例:简单基准测试
start="${EPOCHREALTIME}"
# ... 某些操作 ...
sleep 0.5
end="${EPOCHREALTIME}"
duration=$(awk "BEGIN {print ${end} - ${start}}")
echo "操作耗时 ${duration} 秒"

# 示例:使用精确时间戳记录日志
log_message() {
    printf '[%s.%06d] %s\n' \
        "$(date -d "@${EPOCHSECONDS}" '+%Y-%m-%d %H:%M:%S')" \
        "${EPOCHREALTIME#*.}" \
        "$*"
}

log_message "启动进程"

使用场景:

  • 精确的性能测量
  • 日志的高分辨率时间戳
  • 避免调用外部 date 命令
  • 脚本中的微秒级计时

SRANDOM 变量

密码学强度随机数:

# 生成安全随机数(如果可用)
if [[ -n "${SRANDOM}" ]]; then
    secure_random="${SRANDOM}"
    echo "安全随机数:${secure_random}"
else
    echo "SRANDOM 不可用(需要 getrandom/getentropy)"
fi

# 实用示例:使用安全随机数生成临时文件名
temp_file="/tmp/secure_${SRANDOM}_${EPOCHSECONDS}.tmp"

注意: SRANDOM 需要系统支持 getrandom() 或 getentropy() 系统调用。

改进的重定向语法

{varname} 重定向

将文件描述符赋值给变量,实现更好的文件句柄管理:

# 打开文件描述符并存储到变量中
exec {fd}< input.txt
while IFS= read -r -u "${fd}" line; do
    echo "读取:${line}"
done
exec {fd}<&-  # 关闭文件描述符

# 实用示例:多个文件句柄
exec {input_fd}< data.txt
exec {output_fd}> results.txt
exec {error_fd}> errors.txt

process_data() {
    while IFS= read -r -u "${input_fd}" line; do
        if validate_line "${line}"; then
            echo "已处理:${line}" >&"${output_fd}"
        else
            echo "错误:${line}" >&"${error_fd}"
        fi
    done
}

process_data
exec {input_fd}<&-
exec {output_fd}>&-
exec {error_fd}>&-

优势:

  • 命名文件描述符取代魔法数字
  • 自动 FD 分配避免冲突
  • 更易读、更易维护的代码
  • 更轻松地跟踪已打开的文件句柄

数组增强

改进的关联数组处理

更好地支持复杂数组操作:

# 使用 typeset 声明关联数组
declare -A config=(
    [host]="localhost"
    [port]="8080"
    [debug]="true"
)

# 增强的数组展开
for key in "${!config[@]}"; do
    printf '%s=%s\n' "${key}" "${config[${key}]}"
done

# 实用示例:配置解析器
parse_config() {
    declare -gA app_config
    local line key value

    while IFS='=' read -r key value; do
        [[ "${key}" =~ ^[[:space:]]*# ]] && continue  # 跳过注释
        [[ -z "${key}" ]] && continue                  # 跳过空行

        # 去除两端空白
        key="${key#"${key%%[![:space:]]*}"}"
        key="${key%"${key##*[![:space:]]}"}"
        value="${value#"${value%%[![:space:]]*}"}"
        value="${value%"${value##*[![:space:]]}"}"

        app_config["${key}"]="${value}"
    done < config.ini
}

多维数组支持

改进了嵌套数组结构的处理。

代码示例

Readline 8.1 集成

增强的文本编辑和命令行交互:

键绑定和历史记录

# 在 ~/.inputrc 中配置 readline
# 启用不区分大小写的补全
set completion-ignore-case on

# 启用补全的可见统计信息
set visible-stats on

# 立即显示所有补全选项
set show-all-if-ambiguous on

# 为补全匹配启用颜色
set colored-completion-prefix on

改进的历史搜索

# 在脚本或 .bashrc 中
# 绑定 Ctrl+R 进行反向增量搜索(默认行为,但 5.1 中增强)
bind '"\C-r": reverse-search-history'

# 绑定 Ctrl+S 进行正向增量搜索
bind '"\C-s": forward-search-history'

# 改进的历史处理
shopt -s histappend           # 追加到历史文件
shopt -s cmdhist              # 多行命令作为单条记录
HISTCONTROL=ignoreboth        # 忽略重复项和以空格开头的命令
HISTSIZE=10000
HISTFILESIZE=20000

信号处理改进

子 Shell 和进程替换中更好的信号传播:

# 子 Shell 中增强的 trap 处理
cleanup() {
    echo "正在清理..." >&2
    rm -f "${temp_file}"
    exit 130  # 128 + SIGINT
}

trap cleanup SIGINT SIGTERM

temp_file=$(mktemp)

# 信号通过管道正确传播
long_running_task | process_output &
pid=$!

# 等待后台任务并正确处理信号
wait "${pid}" 2>/dev/null
exit_code=$?

if [[ ${exit_code} -gt 128 ]]; then
    signal=$((exit_code - 128))
    echo "进程被信号 ${signal} 终止" >&2
fi

Bug 修复和边界情况

子 Shell 处理

创建子 Shell 时行为得到改善:

# 更可靠的子 Shell 变量继承
outer_var="parent"

(
    # 子 Shell 现在更可靠地继承父变量
    echo "在子 Shell 中:${outer_var}"
    inner_var="child"
)

# outer_var 仍然可访问,inner_var 不可访问
echo "子 Shell 之后:${outer_var}"

模式匹配边界情况

修复了 glob 模式匹配中的边界情况:

# 更可靠的包含特殊字符的 glob 匹配
shopt -s nullglob  # 无匹配的 glob 展开为空数组
shopt -s extglob   # 扩展模式匹配

# 示例:匹配文件,优雅处理无匹配情况
files=(*.txt)
if [[ ${#files[@]} -eq 0 ]]; then
    echo "未找到 .txt 文件"
else
    printf '找到:%s\n' "${files[@]}"
fi

性能改进

  • 循环中变量展开速度更快
  • 优化的数组操作
  • 大数组的内存使用减少
  • 模式匹配效率提升

兼容性说明

从 Bash 5.0 升级

大多数脚本兼容,但请注意:

  • EPOCHSECONDS 和 EPOCHREALTIME 是新增项——如果面向旧版本,请检查其是否存在
  • SRANDOM 可能并非在所有系统上都可用
  • 修复了展开行为中的某些晦涩边界情况

版本检查

# 使用 Bash 5.1 功能前检查版本
if [[ "${BASH_VERSINFO[0]}" -ge 5 ]] && [[ "${BASH_VERSINFO[1]}" -ge 1 ]]; then
    # 可安全使用 Bash 5.1 功能
    timestamp="${EPOCHSECONDS}"
else
    # 旧版本的回退方案
    timestamp=$(date +%s)
fi

参考资料

其他资源

关于更广泛的 Bash 开发模式和实践,请参阅:

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-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-development

This skill should be used when the user asks to "write a bash script", "create a shell script", "implement bash function", "parse arguments in bash", "handle errors in bash", or mentions bash development, shell scripting, script templates, or modern bash patterns.

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).

相关技能