shellcheck-configuration について
ShellCheck でシェルスクリプトを解析・設定する開発者向けのガイドです。警告への対処、プロジェクトのルール設定、CIやコミット前チェックへの統合を扱います。
主な機能
- 01対象シェルとルールの設定
- 02一般的な警告コードの解説
- 03誤検知と警告抑制への対処
- 04CIとコミット前チェックへの統合
- 05解析出力形式の選択
活用シーン
- 01プロジェクトの ShellCheck ルール設定
- 02CIでのシェルスクリプト検査
- 03警告の調査と修正
- 04対象シェルに応じた移植性の確認
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ ovachiever/ droid-tings --skill shellcheck-configurationインストール後、Claude Code で/shellcheck-configurationを入力して読み込まれたか確認します。
詳細
ShellCheck の設定と静的解析
ShellCheck を設定して使用し、シェルスクリプトの品質を高め、よくある落とし穴を検出し、静的コード解析によってベストプラクティスを徹底するための包括的なガイドです。
このスキルを使用する場面
- CI/CD パイプラインでシェルスクリプトのリントを設定する
- 既存のシェルスクリプトを解析して問題を見つける
- ShellCheck のエラーコードや警告を理解する
- プロジェクト固有の要件に合わせて ShellCheck を設定する
- ShellCheck を開発ワークフローに統合する
- 誤検知を抑制し、ルールセットを設定する
- 一貫したコード品質基準を徹底する
- 品質ゲートを満たすようにスクリプトを移行する
ShellCheck の基本
ShellCheck とは
ShellCheck は、シェルスクリプトを解析して問題のあるパターンを検出する静的解析ツールです。以下をサポートしています。
- Bash、sh、dash、ksh、その他の POSIX シェル
- 100 種類を超える警告とエラー
- 対象シェルやフラグの設定
- エディターや CI/CD システムとの統合
インストール
# macOS with Homebrew
brew install shellcheck
# Ubuntu/Debian
apt-get install shellcheck
# From source
git clone https://github.com/koalaman/shellcheck.git
cd shellcheck
make build
make install
# Verify installation
shellcheck --version
設定ファイル
.shellcheckrc(プロジェクトレベル)
プロジェクトのルートに .shellcheckrc を作成します。
# Specify target shell
shell=bash
# Enable optional checks
enable=avoid-nullary-conditions
enable=require-variable-braces
# Disable specific warnings
disable=SC1091
disable=SC2086
環境変数
# Set default shell target
export SHELLCHECK_SHELL=bash
# Enable strict mode
export SHELLCHECK_STRICT=true
# Specify configuration file location
export SHELLCHECK_CONFIG=~/.shellcheckrc
よくある ShellCheck のエラーコード
SC1000-1099:パーサーエラー
# SC1004: Backslash continuation not followed by newline
echo hello\
world # Error - needs line continuation
# SC1008: Invalid data for operator `=='
if [[ $var = "value" ]]; then # Space before ==
true
fi
SC2000-2099:シェルに関する問題
# SC2009: Consider using pgrep or pidof instead of grep|grep
ps aux | grep -v grep | grep myprocess # Use pgrep instead
# SC2012: Use `ls` only for viewing. Use `find` for reliable output
for file in $(ls -la) # Better: use find or globbing
# SC2015: Avoid using && and || instead of if-then-else
[[ -f "$file" ]] && echo "found" || echo "not found" # Less clear
# SC2016: Expressions don't expand in single quotes
echo '$VAR' # Literal $VAR, not variable expansion
# SC2026: This word is non-standard. Set POSIXLY_CORRECT
# when using with scripts for other shells
SC2100-2199:引用符に関する問題
# SC2086: Double quote to prevent globbing and word splitting
for i in $list; do # Should be: for i in $list or for i in "$list"
echo "$i"
done
# SC2115: Literal tilde in path not expanded. Use $HOME instead
~/.bashrc # In strings, use "$HOME/.bashrc"
# SC2181: Check exit code directly with `if`, not indirectly in a list
some_command
if [ $? -eq 0 ]; then # Better: if some_command; then
# SC2206: Quote to prevent word splitting or set IFS
array=( $items ) # Should use: array=( $items )
SC3000-3999:POSIX 準拠に関する問題
# SC3010: In POSIX sh, use 'case' instead of 'cond && foo'
[[ $var == "value" ]] && do_something # Not POSIX
# SC3043: In POSIX sh, use 'local' is undefined
function my_func() {
local var=value # Not POSIX in some shells
}
実用的な設定例
最小限の設定(厳格な POSIX)
#!/bin/bash
# Configure for maximum portability
shellcheck \
--shell=sh \
--external-sources \
--check-sourced \
script.sh
開発用の設定(ルールを緩和した Bash)
#!/bin/bash
# Configure for Bash development
shellcheck \
--shell=bash \
--exclude=SC1091,SC2119 \
--enable=all \
script.sh
CI/CD 統合用の設定
#!/bin/bash
set -Eeuo pipefail
# Analyze all shell scripts and fail on issues
find . -type f -name "*.sh" | while read -r script; do
echo "Checking: $script"
shellcheck \
--shell=bash \
--format=gcc \
--exclude=SC1091 \
"$script" || exit 1
done
プロジェクト用の .shellcheckrc
# Shell dialect to analyze against
shell=bash
# Enable optional checks
enable=avoid-nullary-conditions,require-variable-braces,check-unassigned-uppercase
# Disable specific warnings
# SC1091: Not following sourced files (many false positives)
disable=SC1091
# SC2119: Use function_name instead of function_name -- (arguments)
disable=SC2119
# External files to source for context
external-sources=true
統合パターン
pre-commit フックの設定
#!/bin/bash
# .git/hooks/pre-commit
#!/bin/bash
set -e
# Find all shell scripts changed in this commit
git diff --cached --name-only | grep '\.sh$' | while read -r script; do
echo "Linting: $script"
if ! shellcheck "$script"; then
echo "ShellCheck failed on $script"
exit 1
fi
done
GitHub Actions ワークフロー
name: ShellCheck
on: [push, pull_request]
jobs:
shellcheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run ShellCheck
run: |
sudo apt-get install shellcheck
find . -type f -name "*.sh" -exec shellcheck {} \;
GitLab CI パイプライン
shellcheck:
stage: lint
image: koalaman/shellcheck-alpine
script:
- find . -type f -name "*.sh" -exec shellcheck {} \;
allow_failure: false
ShellCheck 違反への対処
特定の警告を抑制する
#!/bin/bash
# Disable warning for entire line
# shellcheck disable=SC2086
for file in $(ls -la); do
echo "$file"
done
# Disable for entire script
# shellcheck disable=SC1091,SC2119
# Disable multiple warnings (format varies)
command_that_fails() {
# shellcheck disable=SC2015
[ -f "$1" ] && echo "found" || echo "not found"
}
# Disable specific check for source directive
# shellcheck source=./helper.sh
source helper.sh
よくある違反と修正方法
SC2086:単語分割を防ぐために二重引用符を付ける
# Problem
for i in $list; do done
# Solution
for i in $list; do done # If $list is already quoted, or
for i in "${list[@]}"; do done # If list is an array
SC2181:終了コードを直接確認する
# Problem
some_command
if [ $? -eq 0 ]; then
echo "success"
fi
# Solution
if some_command; then
echo "success"
fi
SC2015:&& || の代わりに if-then を使用する
# Problem
[ -f "$file" ] && echo "exists" || echo "not found"
# Solution - clearer intent
if [ -f "$file" ]; then
echo "exists"
else
echo "not found"
fi
SC2016:シングルクォート内では式が展開されない
# Problem
echo 'Variable value: $VAR'
# Solution
echo "Variable value: $VAR"
SC2009:grep の代わりに pgrep を使用する
# Problem
ps aux | grep -v grep | grep myprocess
# Solution
pgrep -f myprocess
パフォーマンスの最適化
複数ファイルをチェックする
#!/bin/bash
# Sequential checking
for script in *.sh; do
shellcheck "$script"
done
# Parallel checking (faster)
find . -name "*.sh" -print0 | \
xargs -0 -P 4 -n 1 shellcheck
結果をキャッシュする
#!/bin/bash
CACHE_DIR=".shellcheck_cache"
mkdir -p "$CACHE_DIR"
check_script() {
local script="$1"
local hash
local cache_file
hash=$(sha256sum "$script" | cut -d' ' -f1)
cache_file="$CACHE_DIR/$hash"
if [[ ! -f "$cache_file" ]]; then
if shellcheck "$script" > "$cache_file" 2>&1; then
touch "$cache_file.ok"
else
return 1
fi
fi
[[ -f "$cache_file.ok" ]]
}
find . -name "*.sh" | while read -r script; do
check_script "$script" || exit 1
done
出力形式
デフォルト形式
shellcheck script.sh
# Output:
# script.sh:1:3: warning: foo is referenced but not assigned. [SC2154]
GCC 形式(CI/CD 用)
shellcheck --format=gcc script.sh
# Output:
# script.sh:1:3: warning: foo is referenced but not assigned.
JSON 形式(解析用)
shellcheck --format=json script.sh
# Output:
# [{"file": "script.sh", "line": 1, "column": 3, "level": "warning", "code": 2154, "message": "..."}]
静音形式
shellcheck --format=quiet script.sh
# Returns non-zero if issues found, no output otherwise
ベストプラクティス
- CI/CD で ShellCheck を実行する - マージ前に問題を検出します
- 対象シェルに合わせて設定する - bash を sh として解析しないでください
- 除外理由を記録する - 違反を抑制する理由を説明します
- 違反に対処する - 警告を無効にするだけにしないでください
- 厳格モードを有効にする - 慎重に除外を設定したうえで
--enable=allを使用します - 定期的に更新する - 新しいチェックを利用できるよう、ShellCheck を最新に保ちます
- pre-commit フックを使用する - プッシュ前にローカルで問題を検出します
- エディターと統合する - 開発中にリアルタイムでフィードバックを得ます
リソース
- ShellCheck GitHub: https://github.com/koalaman/shellcheck
- ShellCheck Wiki: https://www.shellcheck.net/wiki/
- エラーコードのリファレンス: https://www.shellcheck.net/
コメント
Markdown 対応