git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Why /usr/bin/env in scripts

From
David A. Wheeler <dwheeler@dwheeler.com>
Date
Apr 25, 2005, 17:45 UTC
Message-ID
<426D2CAF.7080101@dwheeler.com>
In-Reply-To
<426C9E63.4050907@mindspring.com>
Philip Pokorny wrote:
Show 7 quoted lines
> 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
Previous: David Greaves
Message 3 of 3 in “Why /usr/bin/env in scripts”
  1. Philip PokornyApr 25, 2005
  2. David GreavesApr 25, 2005
  3. David A. WheelerApr 25, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.