下林明正のブログ

個人的かつ雑多なブログです。

/bin/sh について

多くの場合/bin/shbashであることが多いようで、例えば手元のMacで見てみると、

$ /bin/sh --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)
Copyright (C) 2007 Free Software Foundation, Inc.

となっている。 一方、DebianUbuntuではdashというシェルが採用されているようだ。

man bashしてみると、

If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.

と書いてあり、shとしてbashが起動されるとなるべくそれっぽく振る舞いますよと書かれてある。

ただこれが結構曲者で、実際には普通にfunction句などを使えてしまうのでbashに依存したシェルスクリプトを書けてしまう。

bash依存のシェルスクリプトを書いてしまっていないかどうかはcheckbashismsというコマンドで確認することができる。

ポータビリティを重視するなら気を付けましょう、という学びが得られた。