メインコンテンツへスキップ
kitlocal.dev

Bashチートシート

ローカルで実行

100% ブラウザ内で処理されます。入力や出力が端末の外に送信されることはありません。

GNU Bash スクリプティングと対話ショートカット:パラメータ展開、配列、ループ、条件式、リダイレクト、Bash 5.3 で検証済みのジョブ制御。

動作の仕組み

Bash parses a line in phases: expansions ($var, $(cmd), globs) happen before the command runs, and whitespace splits unquoted expansions into separate words. Double-quoting "$var" keeps spaces intact while still expanding variables; single quotes disable all expansion.

サバイバルキット

最初に覚えるべき最小限のコマンド。

41 件のコマンド

キーボードショートカット/検索にフォーカスJK選択を移動C選択項目をコピーF選択項目をブックマークEsc検索をクリア

Script safety & quoting

Start scripts with strict error checking and quote variable expansions to prevent silent failures.

公式ドキュメント: The Set Builtin
  • 基本

    Exit on errors, unset variables and failed pipeline commands.

    set -euo pipefail

    使いどころ

    Prevents scripts from charging ahead after an earlier failure or blank variable expansion.

    コマンドの内訳

    -e
    exit immediately when a command returns non-zero
    -u
    treat referencing an unset variable as an error
    -o pipefail
    fail a pipeline if any stage fails, not just the last

    実例

    Place `set -euo pipefail` at the top of scripts to fail fast with exit code 1.

    bash -c 'set -euo pipefail; echo "$UNSET_VAR"'

    実行結果

    bash: line 1: UNSET_VAR: unbound variable

    注意点

    • Guard commands expected to return non-zero (like grep with no matches) with `|| true` or an `if` check.

    関連項目

Variables & parameter expansion

Use ${...} parameter expansions for defaults, slicing and path stripping without spawning external tools.

公式ドキュメント: Shell Parameter Expansion

Conditionals & tests

Prefer [[ ... ]] over [ ... ] in Bash scripts: it avoids word splitting and supports regex matching with =~.

公式ドキュメント: Conditional Constructs

Loops & safe file reading

Never parse `ls` in a `for` loop. Use globs for filenames and `while IFS= read -r` for lines.

公式ドキュメント: Looping Constructs

Redirection, pipes & process substitution

FD 1 is stdout (normal output), FD 2 is stderr (errors). Order matters when combining them.

公式ドキュメント: Redirections

Functions, shortcuts & job control

Use readline shortcuts in your terminal and background/foreground jobs for long tasks.

公式ドキュメント: Job Control

KitLocalツールで試す

用語集
Word splitting
Unquoted $var expansions split on spaces/tabs/newlines (IFS), breaking filenames with spaces.
Exit status ($?)
Integer 0..255 returned by the last command. 0 means success; any non-zero value means failure.
Parameter expansion
Transforming variables inside ${...} without spawning sed/awk subprocesses.
Subshell
A child shell created by (...) or pipes where variable changes do not affect the parent shell.

2026/11/15 に検証済み · GNU bash 5.3 で検証済み

出典: GNU Bash Reference Manual · ShellCheck Wiki (common pitfalls)

山括弧内の内容は実際の値に置き換えてください。コマンドは公式ドキュメントに基づいており、実例は記載されたバージョンで実行確認されています。