[CDBUG-talk] Sysadmin blog

Brian Callahan bcallah at devio.us
Sun Nov 16 22:24:38 EST 2014

On 11/16/14 21:59, Jaime wrote:
> On Sun, Nov 16, 2014 at 9:39 PM, Brian Callahan <bcallah at devio.us> wrote:
>> So this reminds me of an oft-forgotten point in portability of shell
>> scripts: the popularity of the linuxism #!/bin/bash (though it appears it
>> has crept into Mac OS X as well, sad...).
> I was aware of the down sides of hard-coding the path to the script's
> interpreter, but I wasn't aware of an alternative.  So thanks for
> sharing the /usr/bin/env tip.  I have two thoughts that I hope you're
> willing to hash-out with me, though.
> 1)  Isn't the shebang that you suggested still hardcoded?  When the
> script is taken to a new OS, wouldn't there still be a risk of it
> breaking?  Has env just been in Unix for so long that its always in
> /usr/bin?

You're right: env is not guaranteed to be in /usr/bin but it is the 
historical location of env, so it will work on Linuxes and BSDs (and 
afaik, Solaris/AIX/HP-UX). Pretty much it's an anomaly if env isn't in 
/usr/bin and it's also extremely likely bash, if available, wouldn't be 
in /bin on such a system anyway!

> 2)  On MacOS, bash is always installed at /usr/bin/bash.  My script
> uses utilities specific to MacOS to modify configuration files
> specific to MacOS in order to ease MacOS deployment.  It is inherently
> irrelevent to other OSs and unable to move to another OS.  So is there
> a benefit to changing the shebang to env?

Because env searches your PATH, you could install a newer/custom bash 
(remember shellshock?) elsewhere and have your scripts use that new bash 
without editing the script (provided your PATH is set up to hit the new 
bash first).


More information about the CDBUG-talk mailing list