4 ms·
> 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
by jwilk 8y ago
> 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.html http://pubs.opengroup.org/onlinepubs/009695399/utilities/sh....
A pedantically-compliant shell script should not have shebang at all.
- Twisol 8y agoWhat's wrong with `#!/usr/bin/env sh`?
- dwheeler 8y agoPOSIX doesn't include shebang (#!) at all. It should, but it currently doesn't.
- oblio 8y ago> currently doesn’t Yeah, we should be gentle with POSIX, it’s only 30 years young :)
- xyzzyz 8y agoNewest revision is from 2017, so they certainly had time to fix that, if they consider it a problem.
- jwilk 8y agoIt doesn't include /usr/bin/ either. The "env" utility could be somewhere else.
- _kst_ 8y agoI'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).
- jwilk 8y agohttps://www.in-ulm.de/~mascheck/various/shebang/#env https://www.in-ulm.de/~mascheck/various/shebang/#env mentions other OSes that have only /bin/env, although admittedly they are all quite old.
- dredmorbius 8y agoTermux rewrites scripts via termux-fix-shebang.
- rkeene2 8y agoIt now also does rewriting on the fly for certain shebang lines, as part of its C library, so unmodified scripts can run.
- dredmorbius 8y agoI didn't know that, thanks.
- rkeene2 8y agoNo 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).
- dredmorbius 8y agoTry that where env ain't there. Android, motherfuckers.
- xyproto 8y ago"/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".
- jackewiehose 8y ago> 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.
- jwilk 8y ago> 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_chap02.html http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu...
- xyproto 8y agoDistros 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.