bash
できること
このガイドは、Linuxのターミナル(Bash)向けのコマンドやスクリプトを、より安全かつ効率的に書く方法を解説します。よくあるエラーを避け、ベストプラクティスを活用する例を紹介します。
使う場面
- ✓Linuxで作業を自動化するスクリプトを書くとき。
- ✓既存のBashコマンドのエラーを修正しようとするとき。
- ✓ターミナルをより高度に使えるようになりたいとき。
スキルのリンク
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/super-code/bash/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“Bashスクリプトで、空白を含むファイル名を正しく扱うにはどうすればよいですか?”
毎週5つの新しいスキル。1分で読めます。
仕組み
このガイドでは、「悪い」コードと「良い」コードを比較して、ベストプラクティスを示します。変数、条件分岐、ループ、コマンド同士の連携の扱い方に焦点を当てています。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
Bash / Shell: Idiomatic Efficiency Reference
When to Use
- Use this skill when the task matches this description: Language-specific super-code guidelines for bash.
Table of Contents
- Quoting & Word Splitting
- Conditionals & Tests
- Loops & Iteration
- Pipes & Process Substitution
- Functions & Return Values
- Error Handling
- Anti-patterns specific to Bash
1. Quoting & Word Splitting {#quoting}
# ❌ Unquoted variable (word splitting + globbing)
for f in $files; do rm $f; done
# ✅
for f in "${files[@]}"; do rm -- "$f"; done
# ❌ Unquoted command substitution
path=$(find . -name config)
cat $path # breaks on spaces
# ✅
path="$(find . -name config)"
cat "$path"
# ❌ Using backticks for command substitution
result=`echo hello`
# ✅ — $() nests cleanly
result=$(echo hello)
# ❌ String comparison without quotes
if [ $var = "hello" ]; then # breaks if var is empty or has spaces
# ✅
if [[ "$var" = "hello" ]]; then
Rule: double-quote every $variable and $(command) unless you specifically need splitting.
2. Conditionals & Tests {#conditionals}
# ❌ Single bracket test (POSIX but fragile)
if [ -f "$file" -a -r "$file" ]; then
# ✅ — [[ is safer, supports &&/||, no word splitting inside
if [[ -f "$file" && -r "$file" ]]; then
# ❌ Testing command exit status with if [ $? -eq 0 ]
grep -q pattern file
if [ $? -eq 0 ]; then echo "found"; fi
# ✅ — test command directly
if grep -q pattern file; then echo "found"; fi
# ❌ String equality with == outside [[
if [ "$a" == "$b" ]; then # == not POSIX in [ ]
# ✅
if [[ "$a" == "$b" ]]; then # Bash
# or POSIX:
if [ "$a" = "$b" ]; then
# ❌ Arithmetic with [ ] and string comparison
if [ "$count" -gt 10 ]; then
# ✅ — (( )) for arithmetic
if (( count > 10 )); then
3. Loops & Iteration {#loops}
# ❌ Parsing ls output
for f in $(ls *.txt); do process "$f"; done
# ✅ — glob directly
for f in *.txt; do
[[ -e "$f" ]] || continue # handle no-match
process "$f"
done
# ❌ Reading file line-by-line with for
for line in $(cat file.txt); do # splits on words, not lines
# ✅
while IFS= read -r line; do
process "$line"
done < file.txt
# ❌ Seq for counting
for i in $(seq 1 10); do
# ✅ — brace expansion (Bash)
for i in {1..10}; do
# or C-style:
for (( i = 1; i <= 10; i++ )); do
# ❌ Processing command output line-by-line with pipe (subshell trap)
count=0
cat file.txt | while read -r line; do
(( count++ )) # count resets after loop — subshell
done
echo "$count" # always 0
# ✅ — redirect, not pipe
count=0
while IFS= read -r line; do
(( count++ ))
done < file.txt
echo "$count"
4. Pipes & Process Substitution {#pipes}
# ❌ Chained grep | grep for AND
grep "error" log.txt | grep "timeout"
# ✅ — single grep with pattern
grep -E "error.*timeout|timeout.*error" log.txt
# or awk for complex logic:
awk '/error/ && /timeout/' log.txt
# ❌ cat + pipe (UUOC — Useless Use of Cat)
cat file.txt | grep pattern
# ✅
grep pattern file.txt
# ❌ Temp file for diff between commands
cmd1 > /tmp/a.txt
cmd2 > /tmp/b.txt
diff /tmp/a.txt /tmp/b.txt
rm /tmp/a.txt /tmp/b.txt
# ✅ — process substitution
diff <(cmd1) <(cmd2)
# ❌ Ignoring pipe failures (only last command's exit code)
false | true
echo $? # 0 — false's failure hidden
# ✅
set -o pipefail
false | true
echo $? # 1
5. Functions & Return Values {#functions}
# ❌ Using return for string values
get_name() {
return "Alice" # return is for exit codes (0-255)
}
# ✅ — echo + capture
get_name() {
echo "Alice"
}
name=$(get_name)
# ❌ Global variables modified inside functions
result=""
compute() { result="done"; }
# ✅ — use local, return via stdout
compute() {
local tmp
tmp=$(do_work)
echo "$tmp"
}
result=$(compute)
# ❌ Function keyword (not POSIX)
function my_func {
# ✅
my_func() {
6. Error Handling {#errors}
# ❌ No error handling — script continues after failure
cd /some/dir
rm -rf * # if cd fails, deletes from wrong directory
# ✅
set -euo pipefail
cd /some/dir || { echo "cd failed" >&2; exit 1; }
rm -rf ./*
# ❌ No cleanup on exit
tmpfile=$(mktemp)
# ... script might exit early, leaving tmpfile
…
よくある質問
「Bash」とは何ですか?
Bashは、Linuxのターミナルに入力したコマンドを解釈するプログラムです。あなたとコンピューターの間をつなぐ通訳のようなものです。
「ダブルクォーテーション」はなぜ重要なのですか?
ダブルクォーテーションは、ファイル名や変数の中の空白や特殊文字をBashが誤って解釈するのを防ぎ、エラーを防止します。
「set -euo pipefail」とはどういう意味ですか?
スクリプトの安全性を高める設定です。コマンドが失敗したり、未定義の変数が使われたりした時点で、スクリプトを直ちに停止させます。
詳細
人気
- GitHubのスター
- 48k
- ランキング
- #5886
- ライセンス
- MIT
- 更新日
- 2026/10/15
コメント
- …
このリポジトリの他のスキル
00-andruia-consultant
48kエージェントがAIプロジェクトの立ち上げや改善を支援します。コードを分析し、ツールを提案し、スペイン語で行動計画を作成します。
007
48kエージェントがセキュリティ監査と脅威モデリングを行えるようにします。攻撃や脆弱性からプロジェクトを守るのに役立ちます。
10-andruia-skill-smith
48kエージェントがAIツールの作成や改善を支援します。ツールの動作を定義し、ドキュメントを書き、構造を検証します。
20-andruia-niche-intelligence
48kエージェントが特定の業界に関する要約を作成できるようにします。信頼できる情報源を使って、プロジェクトの背景、ルール、利用パターン、リスクを把握するのに役立ちます。
似ているスキル
diffs
394k編集内容の実際の共有可能な差分を、手書きの要約ではなく、ビューアーのリンクや画像ファイルとして作成するようエージェントを支援します。
coding-agent
394kCodex、Claude Code、OpenCodeに大きめのコーディング作業をバックグラウンドのワーカーとして渡し、その進捗を確認するようエージェントを支援します。
gh-issues
394kGitHubのイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。