The whole shebang

For some reason the Ubuntu Linux distribution (or possibly Debian, on which it is based) doesn’t lie about /bin/sh. Fedora used to lie, and pretend that /bin/bash was /bin/sh. That way your stock /bin/sh still had functions and other post-Version 7 Bourne Shell features in your stock shell. But, as I say, Ubuntu doesn’t do that.

The reason this matters is that I have a zillion shell scripts that begin with the standard #! shebang-notation, and the shell I specify in them is /bin/sh. So now they’re all broken. (OK, maybe not all, but I tend to use functions a lot to simplify maintenance. Usually a shell script works for a year or two before breaking, and when that happens, it’s important that your scripts be simple to maintain. And for me, that means functions with obvious names instead of blocks of linear code.)

Now you could argue that Fedora was wrong. So I should just fix the #! lines to use /bin/bash and go forward from there. Since I got rid of Fedora, this should be part of moving to Ubuntu. Suck it up and, que no?

But Fedora isn’t alone. Because Mac OS X 10.3+ does the same thing. (Actually the mac does something even stranger: it puts two copies of bash in /bin and calls one of them sh. Have they ever heard of symlinks, or even hard links?).

So I have two choices. I can fix things on Ubuntu and the mac by changing all the lines to be /bin/bash. But then what if I want to move to, say, HP-UX or Solaris, huh? So my other choice is to use #!/usr/bin/env bash as my shebang line.

The latter solution isn’t as efficient as the former, of course. I timed it on my mac and found it takes 6.88s to do a thousand shell-script invocations using /bin/bash, and 10.31s using /usr/bin/env. Doing 10 takes 0.103s and 0.138s, so the ratio appears to be the same even for small values of n. Even if that .035s is constant below 10, I can wait 35/1000 of a second each time I invoke a shell.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.