Communitygithub.com

Bash 5.3 の新機能と改善点

Bash 5.3 のリリース機能と改善点を実践的な例と共に解説します。Bash 5.3 の新機能、新しいコマンド置換、GLOBSORT、ローダブル組み込み関数、または Bash 5.3 の変更点、新機能、バージョン固有の機能についてユーザーが質問した際に使用します。

Bash 5.3 の新機能と改善点 とは?

このスキルは、2025年7月にリリースされたBash 5.3で導入された主要な新機能と改善点を網羅的に解説します。主な新機能には、サブシェルのフォークを回避してパフォーマンスを劇的に向上させる革命的なコマンド置換 `${ command; }`、出力を自動的に`REPLY`変数に格納する`${| command; }`、ファイル名のソート順を制御する`GLOBSORT`変数、新しいローダブル組み込み関数(`kv`、`strptime`、`fltexpr`)などが含まれます。また、`compgen`、`read`(Readline補完`-E`)、`source`(パス指定`-p`)、`printf`の拡張、POSIXモードの改善、エラー報告の強化、C標準準拠の向上についても解説しています。各機能の説明には実践的なコード例とベンチマークが含まれており、開発者がBash 5.3の利点を最大限に活用できるように設計されています。このスキルは、Bash 5.3の新機能を活用したい開発者や、レガシースクリプトをBash 5.3に移行する際のガイドとして有用です。

対応✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Jamie-BitFlight/claude_skills/tree/main/plugins/bash-development/skills/bash-53-features

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

Bash 5.3 の新機能と改善点 は何をしますか?

2025年7月にリリースされたBash 5.3は、画期的なコマンド置換構文、新しい変数、ローダブル組み込み関数、およびC標準準拠の改善など、重要な機能強化を導入しています。

革命的なコマンド置換

効率的なシェル内実行: ${ command; }

フォークせずにコマンドを実行し、パフォーマンスを劇的に向上させます:

# 従来のコマンド置換(サブシェルを作成)
result=$(echo "Hello, World")

# 新: シェル内コマンド置換(フォークなし!)
# 注: '{' の後ろには空白(タブ/改行/|)が必要です
result=${ echo "Hello, World"; }

# 実践例: 高速な変数代入
config_value=${ grep "^timeout=" config.txt | cut -d= -f2; }

# パフォーマンス比較関数
benchmark_substitution() {
    local i
    local start end

    echo "従来の置換をテスト中..."
    start="${EPOCHREALTIME}"
    for ((i = 0; i < 1000; i++)); do
        result=$(echo "${i}")
    done
    end="${EPOCHREALTIME}"
    printf '従来: %.4f 秒\n' \
        "$(awk "BEGIN {print ${end} - ${start}}")"

    echo "新しいシェル内置換をテスト中..."
    start="${EPOCHREALTIME}"
    for ((i = 0; i < 1000; i++)); do
        result=${ echo "${i}"; }
    done
    end="${EPOCHREALTIME}"
    printf 'シェル内: %.4f 秒\n' \
        "$(awk "BEGIN {print ${end} - ${start}}")"
}

benchmark_substitution

利点:

  • サブプロセス生成のオーバーヘッドなし
  • 単純なコマンドで大幅に高速化
  • ループ内でのリソース使用量の削減
  • 従来のコマンド置換と同じ構文で直感的

REPLY変数キャプチャ: ${| command; }

コマンドを実行し、出力を自動的に REPLY に格納します。注: REPLY は置換内でローカル変数です。その値は完了後に復元されるため、すぐにキャプチャしてください:

コード例

利点:

  • 中間変数を使わないクリーンなコード
  • 一貫したキャプチャメカニズム
  • 視覚的な乱雑さの軽減
  • 迅速なプロトタイピングに最適

ユースケース比較

# 文字列操作 - 効率性のためにシェル内置換を使用
filename="document.txt"
basename=${ echo "${filename%.*}"; }
extension=${ echo "${filename##*.}"; }

# 出力キャプチャ - 明確さのために REPLY を使用
${| df -h / | tail -1 | awk '{print $5}'; }
disk_usage="${REPLY}"
echo "ディスク使用率: ${disk_usage}"

# 複雑なパイプライン - 従来の方法がまだ明確な場合もある
result=$(cat file.txt | grep pattern | sort | uniq)

GLOBSORT 変数

ファイル名およびパス名展開のソート順を制御します。指定子には、オプションで +(昇順、デフォルト)または -(降順)をプレフィックスとして付けることができます:

コード例

利用可能なソート指定子:

  • name — ファイル名のアルファベット順
  • size — ファイルサイズ順
  • mtime — 変更時刻順
  • atime — アクセス時刻順
  • ctime — iノード変更時刻順
  • blocks — 割り当てブロック数順
  • numeric — ファイル名の先頭の数字で数値ソート
  • nosort — ソート無効(グロブ順)
  • プレフィックス: +(昇順、デフォルト)または -(降順)

拡張された組み込み関数

compgen の変数格納

補完結果を直接変数に格納:

コード例

read のReadline補完 (-E)

オートコンプリート機能を備えた対話型入力:

# read中にreadline補完を有効化
choose_file() {
    local file

    echo "ファイル名を入力(タブで補完):"
    read -e -r -E -p "> " file

    if [[ -f "${file}" ]]; then
        echo "選択: ${file}"
        return 0
    else
        echo "ファイルが見つかりません: ${file}"
        return 1
    fi
}

choose_file

# 実践例: 対話型設定
configure_app() {
    local config_file

    echo "設定ファイルを選択:"
    read -e -r -E -p "設定: " config_file

    if [[ -f "${config_file}" ]]; then
        ${| grep -c "^[^#]" "${config_file}"; }
        local line_count="${REPLY}"
        echo "アクティブな設定行が ${line_count} 行見つかりました"
    fi
}

source のパス指定 (-p)

ソーススクリプトの検索パスを指定:

コード例

printf の拡張

マルチバイト文字列と表現に関する新しいオプション:

# 拡張されたprintfオプション
printf '%q\n' "スペースを含む文字列"  # シェル引用符で囲まれた出力

# 新: マルチバイト文字列サポートの改善
text="こんにちは、世界"
printf '長さ: %d バイト\n' "${#text}"

# 実践例: 安全なコマンド構築
build_command() {
    local -a args=("$@")
    local arg cmd=""

    for arg in "${args[@]}"; do
        cmd+=$(printf '%q ' "${arg}")
    done

    echo "安全なコマンド: ${cmd}"
}

build_command ls "-l" "スペースを含むファイル.txt"

新しいローダブル組み込み関数

kv - キー・バリュー配列

キーとバリューのデータから連想配列を作成します。注: kv 組み込み関数の存在は Bash 5.3 で確認されています。以下のインターフェースは参考例です。ロード後に help kv で実際のインターフェースを確認してください:

コード例

strptime - 日付解析

テキスト形式の日付をUnixタイムスタンプに解析します。注: strptime 組み込み関数の存在は Bash 5.3 で確認されています。以下のインターフェースは参考例です。ロード後に help strptime で実際のインターフェースを確認してください:

コード例

fltexpr - 浮動小数点計算

外部ツールなしで浮動小数点演算を実行:

コード例

POSIX モードの改善

POSIX 準拠の強化:

# POSIX モードを有効化
set -o posix

# 文字列比較がロケールルールに従うように
[[ "ä" < "z" ]]  # ロケール依存の比較

# 組み込み関数のPOSIX準拠が改善
# test、trap、wait、bind のすべてがより厳密に準拠

# 実践例: 移植可能なスクリプトヘッダ
#!/usr/bin/env bash

if [[ "${BASH_VERSINFO[0]}" -ge 5 ]] && [[ "${BASH_VERSINFO[1]}" -ge 3 ]]; then
    # Bash 5.3+ が利用可能、最新機能を使用
    USE_MODERN_FEATURES=1
else
    # 移植性のためPOSIXモードにフォールバック
    set -o posix
    USE_MODERN_FEATURES=0
fi

改善されたエラー報告

より詳細なエラーメッセージ:

コード例

C 標準準拠の改善

Bash 5.3 は最新のC標準への準拠を改善しています(ビルド最小要件はC90のままです):

  • K&R C コンパイラではコンパイル不可
  • コードベースの近代化による準拠性の向上
  • 最適化の機会の増加
  • 型安全性の向上

注: これは主にBashをソースからコンパイルする開発者に影響し、エンドユーザーには影響しません。

パフォーマンスの改善

  • フォークなしのコマンド置換(${ cmd; })によるオーバーヘッドの大幅削減
  • GLOBSORT によるグロビングの最適化
  • 高速化された組み込み関数の操作
  • 各種操作におけるメモリ使用量の削減
# ベンチマーク: 従来 vs 新しい置換
benchmark() {
    local iterations=10000
    local i start end

    start="${EPOCHREALTIME}"
    for ((i = 0; i < iterations; i++)); do
        result=$(echo test)
    done
    end="${EPOCHREALTIME}"
    printf '従来: %.4f 秒\n' \
        "$(awk "BEGIN {print ${end} - ${start}}")"

    start="${EPOCHREALTIME}"
    for ((i = 0; i < iterations; i++)); do
        result=${ echo test; }
    done
    end="${EPOCHREALTIME}"
    printf 'シェル内: %.4f 秒\n' \
        "$(awk "BEGIN {print ${end} - ${start}}")"
}

benchmark

移行ガイド

Bash 5.2 から 5.3 へ

ほとんどのスクリプトは互換性がありますが、以下を検討してください:

# パフォーマンス向上のために新しいコマンド置換を活用
# 旧:
for file in *; do
    size=$(stat -f%z "${file}" 2>/dev/null)
done

# 新(より高速):
for file in *; do
    ${| stat -f%z "${file}" 2>/dev/null; }
    size="${REPLY}"
done

# より良いファイル処理のために GLOBSORT を使用
GLOBSORT="-mtime"
for file in *.log; do
    process_recent_log "${file}"
done

# 適切な場所で新しい組み込み関数を活用
# 代わりに: awk 計算
# 使用: fltexpr で浮動小数点演算

バージョン検出

# Bash 5.3 の機能を確認
if [[ "${BASH_VERSINFO[0]}" -ge 5 ]] && [[ "${BASH_VERSINFO[1]}" -ge 3 ]]; then
    echo "Bash 5.3+ の機能が利用可能です"
    CAN_USE_INSITU_SUBSTITUTION=1
    CAN_USE_GLOBSORT=1
else
    echo "Bash バージョン: ${BASH_VERSION}"
    CAN_USE_INSITU_SUBSTITUTION=0
    CAN_USE_GLOBSORT=0
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-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-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).

関連スキル