2
1

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 5 years have passed since last update.

【Linux】シェルスクリプトにおいてShebang に bash を明示する

Posted at

Shebang に bash を明示する

Bash でしか使えない機能のことを俗に Bashism と言います。
Bashism はもちろん Bash 以外のシェルでは動きません。

#!/bin/sh の罠

これに関するありがちな罠は、以下のように発生します。

  • Bash が /bin/sh として使われている環境でシェルスクリプトを書く。
    うっかり Bashism がシェルスクリプトに含まれていても、 /bin/sh は何も文句を言わないので、Bashism に気付かない。
  • /bin/sh が Bash でない環境でそのシェルスクリプトを実行すると動かない。

※ Debianがまさにこのパターンです。 /bin/sh はdebianの場合 Dash

解決策

この解決策は、どの環境でも必ず Bash でシェルスクリプトが実行されるようにすることです。
シェルスクリプトの先頭行には #!/bin/sh ではなく #!/bin/bash と書きましょう。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?