# shebang 行とは何か? インタープリタを選ぶ

> shebang 行は、どのインタープリタで実行するかを OS に伝えるスクリプトの最初の行です。shebang の仕組みと、なぜ CI スクリプトにとって重要なのかを学びましょう。

Source: https://latchkey.dev/ja/learn/ci-explained/what-is-a-shebang-line  
Updated: 2026-06-26

shebang 行は、#! で始まるスクリプトの最初の行で、どのインタープリタがファイルを実行すべきかをオペレーティングシステムに伝えます。

スクリプトを直接実行するとき、オペレーティングシステムはどのプログラムがそれを解釈すべきかを知る必要があります。Bash、Python、Node、あるいは他の何かです。shebang 行がそれに答えます。それは小さいながらも重要な詳細であり、どのシェルまたはインタープリタ、したがってどの機能を CI スクリプトが得るかを決定します。

## shebang の見た目

それはまさに最初の行で、2 文字の #! に続いてパスが来ます。たとえば `#!/bin/bash` や `#!/usr/bin/env python3` です。ファイルを直接実行すると OS がそれを read します。

## どう使われるか

`./script.sh` を実行すると、カーネルは shebang を見て、指定されたインタープリタを起動し、ファイルを渡します。shebang がないと、OS はデフォルトのシェルにフォールバックする可能性があり、それは意図したものではないかもしれません。

## env ベースの shebang

- `#!/bin/bash` は Bash への固定パスを指す。
- `#!/usr/bin/env bash` は PATH 経由で Bash を見つけ、より移植性が高い。
- `#!/usr/bin/env python3` は Python インタープリタに対して同じことを行う。

## shebang が重要な理由

shebang は、スクリプトが `sh` で実行されるか `bash` で実行されるかを決定し、それにより利用可能な機能が変わります。Bash の配列を使うスクリプトは、その shebang がプレーンな POSIX シェルに解決されると壊れます。

## CI における shebang

CI はしばしばスクリプトを明示的に呼び出し (`bash deploy.sh`)、これは shebang をオーバーライドします。しかし step がチェックインされたスクリプトを直接呼び出すときは、shebang がインタープリタを制御するので、正しく設定することでローカルと CI の実行を一貫させられます。

## managed runner での一貫性

Latchkey runner はインタープリタを予測可能な場所に提供するので、`#!/usr/bin/env bash` shebang は毎回同じように解決されます。これにより、直接実行されるスクリプトが CI でも開発者のマシンでも同じように振る舞います。

## Applying this to your pipeline

- Measure before changing. Most CI optimisation targets the wrong step because the slow one is assumed rather than timed.
- Cache what is expensive to produce and cheap to validate, and key the cache to the exact tool version.
- Fail fast: run the cheapest checks that can reject a change first, so an expensive job never starts on code that cannot pass.
- Prefer determinism over speed when they conflict. A fast pipeline nobody trusts gets re-run, which is slower than a slow one that is believed.

## FAQ

### What is What is a shebang Line? choosing the interpreter?

When you run a script directly, the operating system needs to know what program should interpret it: Bash, Python, Node, or something else. The shebang line answers that. It is a small but important detail that decides which shell or interpreter, and therefore which features, your CI script gets.

### What a shebang looks like?

It is the very first line and begins with the two characters #! followed by a path, for example #!/bin/bash or #!/usr/bin/env python3. The OS reads it when you execute the file directly.

### How it is used?

When you run ./script.sh, the kernel sees the shebang, launches the named interpreter, and hands it the file. Without a shebang, the OS may fall back to the default shell, which might not be what you intended.

### Why the shebang matters?

The shebang decides whether your script runs under sh or bash, which changes which features are available. A script using Bash arrays will break if its shebang resolves to a plain POSIX shell.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
