45
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【便利ツール紹介】Googleも推奨!シェルスクリプトを書くなら必ず導入したい静的解析ツール

45
Last updated at Posted at 2026-04-22

株式会社ブレインパッド プロダクトユニットの鈴木です。

弊社は「データ活用の促進を通じて持続可能な未来をつくる」をミッションに、
データ分析支援やSaaSプロダクトの提供を通じて、企業の「データ活用の日常化」を推進しております。

現在、私は企業のマーケティング活動をデータで支援するRtoaster action+の開発を担当しております。

はじめに

「シェルスクリプトを書いてみたけれど、書き方がこれで正しいのか不安」「実行してみて初めてエラーに気づく」といった経験はないでしょうか。

例えば、、、以下のシェルスクリプトにはどのような問題があるでしょうか?

hoge.sh
#!/bin/sh
grep ${VAR} hoge.txt

一つの答えとしては、↑を実行すると↓のように${VAR} が消えて grephoge.txt を「検索パターン」だと認識して、入力を待ち続けてしまいます。(set -uを入れれば気づけるとは思いますが、、、)

$ sh -x hoge.sh
+ grep hoge.txt

本来であれば、↓のように"で囲むべきです。

hoge2.sh
#!/bin/sh
grep "${VAR}" hoge.txt

このようなミスに気づけるツールの紹介です。

ShellCheck

ShellCheckは、シェルの静的解析ツールです。

Googleのコーディング規約において利用することがおすすめされていたり、KubernetesなどのOSSでもCIにShellCheckが組み込まれています。

ShellCheck
The ShellCheck project identifies common bugs and warnings for your shell scripts. It is recommended for all scripts, large or small.
引用:Shell Style Guide

例えば、以下のシェルスクリプトを入力すると、

#!/bin/bash

CURRENT_DIR=`pwd`

if [ $1 == "start" ]; then
  echo Starting in $CURRENT_DIR
fi

for file in $(ls *.txt); do
  cat $file | grep "ERROR"
done

以下のように構文的なエラーだけでなくて、より良い書き方の提案を出してくれます。

利用方法

利用方法は、Web,Editor,Terminal,CIなど様々ありますが、利用方法をいくつか紹介したいと思います。
詳細は、こちらをご覧ください

Web

こちらにアクセスしてチェックしたいスクリプトを入力する

Terminal

Docker(環境を汚したくない場合)

docker run --rm -v "$PWD:/mnt" koalaman/shellcheck:stable test.sh

installする場合

# macOS
brew install shellcheck

# Ubuntu/Debian
sudo apt install shellcheck
$ shellcheck test.sh

In test.sh line 3:
CURRENT_DIR=`pwd`
            ^---^ SC2006 (style): Use $(...) notation instead of legacy backticks `...`.

Did you mean:
CURRENT_DIR=$(pwd)


In test.sh line 5:
if [ $1 == "start" ]; then
     ^-- SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean:
if [ "$1" == "start" ]; then


In test.sh line 6:
  echo Starting in $CURRENT_DIR
                   ^----------^ SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean:
  echo Starting in "$CURRENT_DIR"


In test.sh line 9:
for file in $(ls *.txt); do
            ^---------^ SC2045 (error): Iterating over ls output is fragile. Use globs.
                 ^-- SC2035 (info): Use ./*glob* or -- *glob* so names with dashes won't become options.


In test.sh line 10:
  cat $file | grep "ERROR"
      ^---^ SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean:
  cat "$file" | grep "ERROR"

For more information:
  https://www.shellcheck.net/wiki/SC2045 -- Iterating over ls output is fragi...
  https://www.shellcheck.net/wiki/SC2035 -- Use ./*glob* or -- *glob* so name...
  https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...

VSCode, Cursor

拡張機能で「shellcheck」を入力しインストール

CircleCI

version: '2.1'
orbs:
  shellcheck: circleci/shellcheck@3.1.2
workflows:
  my_workflow:
    jobs:
      - shellcheck/check:
          dir: ./myScripts
          exclude: SC2148 # shebang無しスクリプトの警告を除外

(おまけ)Neovim

(設定は、claudeに書いてもらいましたので画面だけ)

最後に

かなり雑な紹介になりましたが、手軽に導入できるので、IDE、Terminal、CI、pre-commitフックなどに組み込んでみてはいかがでしょうか? また、何かおすすめのツールがあればコメントで共有いただけると幸いです!

45
39
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
45
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?