Subject: Re: Why /usr/bin/env in scripts

From: "David A. Wheeler" <[email-masked]>
Date: Mon, 25 Apr 2005 13:45:19 -0400

Philip Pokorny wrote:

> I notice that the first line of the pasky shell scripts is
>
> #!/usr/bin/env bash
>
> rather than what I'm more familiar with:
>
> #!/bin/bash

...

> So what am I missing?  Is this a portability aid?

Correct.  Bash gets installed in different places on different systems,
e.g., on OpenBSD bash is NOT in /bin or /usr/bin, but in /usr/local/bin.
Invoking "/bin/sh" on OpenBSD will _NOT_ get you bash, but a pdksh
variant that doesn't implement the bash capabilities some scripts require.
Using env this way an old trick; I don't know who originally came up 
with it.

Technically, even this isn't 100% portable.  Leading "#!" (shebang)
isn't implemented by a very few old systems (it's not in the Single User 
Spec,
though it was introduced between v7 and v8 of Unix). Also,
you can find one or two really obscure systems where env isn't in /usr/bin
(OpenServer 5 and a few really old Linux distributions).
But env is ONLY in /usr/bin for the *BSDs.
This version runs on 99.999% of the Linux/Unix systems, and takes
essentially zero effort to implement; "more portable" solutions are so much
more work that few people bother if they're writing shell scripts.

--- David A. Wheeler