POSIX Shebangs – Are Absolute Paths Default?

posixshell

EDIT: The question and the answers are correct, but the problem I stated here is not. The maintainer actually REFUSED to use /usr/bin/env as a solution and instead recreated the script as an sh one, and doing that broke other users installs. So, using env for finding bash is not POSIX, but is default "enough" to be considered a standard.

I'm having some errors on installing software on nixos (specifically the haskell stack ) and after some digging on internet, I found some examples of people having trouble with the specific path of installed software. Here an example where a maintainer reverted a modification to a /usr/bin/env solution on finding bash because it broke other users installations.

So, referring to env on a shebang without informing the absolute path of installation (relying on the system search path) is something that could be considered wrong?

I mean is ok using

#!env bash

instead of

#!/usr/bin/env bash

if the absolute path of installation for env itself is not default on all systems?

Best Answer

env is present in /usr/bin on virtually all Unix-like systems that are still extant in the 21st century. The two exceptions I'm aware of are SCO OpenServer (which still powers a few very old servers) and NextSTEP (there can't be much supported hardware that hasn't died out). For portability, use #!/usr/bin/env. Anything else is less portable (except #!/bin/sh, which carries its own problems and is only useful if you do plan to run your script on SCO).

POSIX and Single Unix don't define /usr/bin/env, or /bin/sh, or any absolute path other than /, /tmp, and a few entries in /dev. /usr/bin/env is not a formal Unix standard, but it is very much a de facto standard.

#!env is completely useless. Shebang lookup doesn't use the PATH variable, it's done by the kernel. (And if it did PATH lookup then you might as well write #!bash directly)

#!/usr/bin/env bash is perfectly fine for a bash script and I question the wisdom of the maintainer who rejected a change from #!/bin/bash. It looks like the maintainer was ruffled by the request and then didn't take it very seriously. Changing to use standard sh instead of bash is even better for portability, but apparently that didn't work (the script probably had a nonstandard construct left). If converting the script to work in other shells was not possible then the script should still have been changed to use #!/usr/bin/env bash instead of #!/bin/bash.