0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

#0014 シバン 【shebang】とは

Last updated at Posted at 2023-08-11

はじめに

  • 最近の仕事ではbashスクリプトを書いてdockerコンテナを一括実行するようなことがあった。
  • bashスクリプトの一行目のおまじないの #!/bin/bash が何なのかが気になったので調べてみた。

シバン (shebang)

  • #!/bin/bashはshebangと言われるもので、LinuxなどのUNIX系OSで実行されるスクリプトファイルで、実行する処理系(インタプリタ)を指定する仕組み。
  • 一行目でインタプリタを指定して、二行目以降の文を引数として渡す。
  • bashのみが使えるものでもなく、pythonファイルの先頭に#!/usr/bin/env pythonと記述すればpythonのインタプリタをしていできます。

参考

shebangの使い方

  • hello.sh
#!/usr/bin/bash
echo hello!!!
  • 実行
❯❯❯ bash hello.sh
hello!!!

shebangがないとどうなるか

  • それでは#!/usr/bin/bashがないとどうなるか...結果は同じ出力がされた......
  • これに関して調べてみると、shebangはなくてもうまくいくことはあるようです。
  • ただ、shebangがないと想定外のインタプリタでスクリプトが実行される可能性があるようで、記述はした方が良いとのことです。(ふわっとした結論ですが...)

おわりに

  • shebangという名前の由来は「ハッシュorシャープ(#)」と「エクスクラメーションマーク(!) ※バンとも呼ばれる」の組み合わせから“hash-bang” あるいは “sharp-bang” を縮めて “shebang”となったらしい。
0
0
1

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?