Community程式設計與開發github.com

Bash 5.1 功能與改進

Bash 5.1 版本的新功能與改進,並附帶實用範例。適用於使用 Bash 5.1 功能、紀元時間變數、重新導向增強功能,或使用者詢問 Bash 5.1 變更、新功能或版本特定能力的情境。

Bash 5.1 功能與改進 是什麼?

這份技能檔案詳細介紹了 Bash 5.1 版本(2020 年 12 月發布)的各項新功能與改進,專為現代 Shell 腳本開發者設計。它涵蓋了兩個新的特殊變數:`EPOCHSECONDS` 和 `EPOCHREALTIME`,可用於取得高精度時間戳記,以及 `SRANDOM` 變數用於生成加密強度的隨機數。此外,檔案說明了改進的重新導向語法,特別是 `{varname}` FD 變數指派,能讓檔案描述符的管理更清晰且可維護。陣列功能也獲得增強,包括更好的關聯式陣列處理與多維陣列支援。Readline 8.1 的整合帶來了更好的按鍵綁定和歷史搜尋功能。訊號處理也有改進,在子 Shell 和行程替代中能更可靠地傳播訊號。此技能檔案還包含錯誤修復、邊界案例的處理、效能提升以及從 Bash 5.0 升級的相容性注意事項。提供的實用範例涵蓋了基准測試、安全暫存檔產生、檔案描述符管理、配置解析和版本檢查。這份指南適用於 Cursor、Claude Code 和 Codex 等 AI 代理程式平台,協助開發者充分利用 Bash 5.1 的最新功能。

相容平台✓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}"

# 取得目前 Unix 時間戳記(微秒精度)
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

錯誤修復與邊界案例

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

相關技能