Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> POSIX mandates /bin/sh

Nope. On the contrary, it says: Applications should note that the standard PATH to the shell cannot be assumed to be either /bin/sh or /usr/bin/sh

Source: http://pubs.opengroup.org/onlinepubs/009695399/utilities/sh....

A pedantically-compliant shell script should not have shebang at all.



> it says: Applications should note that the standard PATH to the shell cannot be assumed to be either /bin/sh or /usr/bin/sh

It also recommends a script: "Installation time script to install correct POSIX shell pathname".

But I wonder how they execute this script. So this is all crap. They should remove that and instead put in something like "you have to test your script on the platform where you deploy.".

The article is missing the one big problem I encountered at most: bin/sh instead of bin/bash or the wrong version of sh|bash.


> But I wonder how they execute this script.

As I mentioned earlier, the trick is to have no shebang.

If the first line of a file of shell commands starts with the characters "#!", the results are unspecified.

[...]

If the execve() function fails due to an error equivalent to the [ENOEXEC] error [...], the shell shall execute a command equivalent to having a shell invoked with the pathname resulting from the search as its first operand, with any remaining arguments passed to the new shell, except that the value of "$0" in the new shell may be set to the command name.

Source: http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu...


What's wrong with `#!/usr/bin/env sh`?


I've worked on systems where env was installed as /bin/env and not as /usr/bin/env . (I think it was SunOS 4.)

For that matter, under Termux on Android it's /data/data/com.termux/files/usr/bin/env (but termux has a hack to make normal shebangs work).


https://www.in-ulm.de/~mascheck/various/shebang/#env mentions other OSes that have only /bin/env, although admittedly they are all quite old.


Termux rewrites scripts via termux-fix-shebang.


It now also does rewriting on the fly for certain shebang lines, as part of its C library, so unmodified scripts can run.


I didn't know that, thanks.


No problem. I wrote it because I version control my scripts and didn't want to fork them for running them on my laptop (Chromebook with Termux).


"/usr/bin/env" is relying on a binary being present in a specific location just as much as relying on a binary being present in "/bin/bash". For most distros and BSDs, "/usr/bin/env" is more likely to be present, but it's not guaranteed for neither "/bin/bash" nor "/usr/bin/env".


POSIX doesn't include shebang (#!) at all. It should, but it currently doesn't.


It doesn't include /usr/bin/ either. The "env" utility could be somewhere else.


> currently doesn’t

Yeah, we should be gentle with POSIX, it’s only 30 years young :)


Newest revision is from 2017, so they certainly had time to fix that, if they consider it a problem.


Try that where env ain't there.

Android, motherfuckers.


Distros that places symlinks to bash in all those locations solves this, and allows all POSIX-compliant shebangs for sh and bash. On Arch Linux, /bin/bash, /bin/sh and /usr/bin/sh all point to /usr/bin/bash.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: