5 ms·
> That means scripts/programs written for other Unixes or other Linuxes and ported to your distribution will no longer need fixing for the file system paths of
by hulitu 4y ago
> That means scripts/programs written for other Unixes or other Linuxes and ported to your distribution will no longer need fixing for the file system paths of the binaries called, which is otherwise a major source of frustration
I don't know what this guy is smoking, but this has never been a problem.
- kazinator 4y agoWhat's been a problem is not being able to pass more than one argument through hash-bang. Fix that one, and you can use #!/usr/bin/env interpreter --arg --arg ... Problem solved; no need to mess with the file system divisions. For that matter ... the stupid hash bang mechanism itself should do PATH resolution, maybe, you know? #!/usr/bin/awk -f <-- find awk at that absolute path #!awk -f <-- Just use the darn PATH, dear hash-bang handling code in the kernel
- nerdponx 4y agoIt's a real problem with interpreted languages in general. PATH lookup is not always desirable. Not to mention library linking and looking for various other data files.
- kazinator 4y agoThe writer of the hash bang script retains the control not to have PATH lookup take place: they write an interpreter name which contains one or more slashes.
- paranoidrobot 4y agoNice, but PATH isn't always set right. I've run into this particular issue with stripped down containers. I run a script as part of the container's CMD/ENTRYPOINT, and it fails. Run it through an interactive shell, and hey-presto PATH is setup and script runs fine.
- mbreese 4y agoI’d suspect the problem is a stripped down container where the profile isn’t setup how you’re expecting. The interactive shell (bash, I assume), will setup the profile/env properly. I’m not sure your entrypoint cmd will setup the environment, as IIRC, the inir process would normally take care of this setup. But an entrypoint in a container has different rules. Perhaps if you started your entrypoint with “/bin/bash cmd”??
- nmz 4y agoQuite frankly if your PATH isn't set up correctly you have bigger problems than #!
- kazinator 4y agoIf PATH isn't set up right then #!/usr/bin/env interpreter -S arg, which everyone uses, also won't work. It doesn't amount to a good argument why #!interpreter arg shouldn't be able to do that path search without the env shim or its need for the -S mechanism.
- anderskaseorg 4y agoThis problem has been solved on at least Linux (GNU coreutils ≥ 8.30, 2018-07-01), FreeBSD (≥ 6.0, 2005-11-04), and macOS with the env -S option: #!/usr/bin/env -S interpreter --arg --arg https://www.gnu.org/software/coreutils/manual/html_node/env-invocation.html#g_t_002dS_002f_002d_002dsplit_002dstring-syntax https://www.gnu.org/software/coreutils/manual/html_node/env-... https://www.freebsd.org/cgi/man.cgi?env https://www.freebsd.org/cgi/man.cgi?env https://www.unix.com/man-page/mojave/1/env/ https://www.unix.com/man-page/mojave/1/env/
- kazinator 4y agoI know about this, and in fact I wrote an alternative solution for GNU Coreutils env in 2017: a complete patch with updated documentation and test cases. Here is a problem: interpreters take arguments such as options ... but, separately, so do interpreted programs. Sometimes, you'd like to control both. I made an extension to env whereby you could do: #!/usr/bin/env :interp:--foo:{}:--bar This would find the "interp" interpreter using a PATH search and then pass it the arguments --foo <scriptname> --bar, followed by arguments that came from the invocation of the hash-bang script. If the special argument {} does not appear, then interp will be invoked with --foo --bar <scriptname>: the script name is not relocated into the arguments. However, the maintainer expressed a preference for compatibility with FreeBSD -S. I took a look at the requirements and saw that FreeBSD's env -S was doing a whole lot of stuff: interpreting C-like character escape sequences like \n, and interpolating dollar-sign-sigiled variables like $USER. In spite of all that, didn't see the feature of being able to insert the script name into the middle of the arguments, like in my solution. It amounted to throwing away my patch and implementing some FreeBSD stuff I had no interest in and a lot of which I thought was a bad idea, while failing to achieve my original functionality. So, I just did the first part: throwing away my patch.
- anderskaseorg 4y agoThe typical use case for env -S is a script passing arguments to the interpreter (#!/usr/bin/env -S interp --foo → interp --foo ./scriptname), and the user can always pass arguments to the script (./scriptname --bar → interp --foo ./scriptname --bar). But why would a script ever need to use a shebang to pass an argument like --bar to itself? Can’t it just modify its own argv, or act as if it had? (I found your message at https://lists.gnu.org/archive/html/coreutils/2017-05/msg00021.html https://lists.gnu.org/archive/html/coreutils/2017-05/msg0002... that claims > It is useful because it allows the hash bang to specify some arguments after the script (which could be arguments belonging to the script rather than to the interpreter, for instance). but doesn’t really explain why the script itself would want to specify that.) In any case, compatibility is important here, in order for it to be possible to write cross-platform scripts.
- nmz 4y agoYou could always fixup all of your scripts so they point to the correct point in PATH. this also has the benefit of notifying you if a program doesn't exist. Do note that env is unportable (freebsd has -P I think), which makes parsing it a PITA, identifying where the program actually is, is slightly challenging. If you ask me its better to throw the program to sh and let it eval to whatever. sh is the true portable launcher. I wrote [0] in lua a while back (mind the comments), didn't destroy anything I think, I wrote [1] just now in gawk, so not as recommended. [0]: https://termbin.com/ijsu https://termbin.com/ijsu [1]: https://termbin.com/rx0e https://termbin.com/rx0e FWIW the correct solution, is the plan9 one, just union mount/bind all paths to /bin.
- xani_ 4y agoIt's funny that you mention that because ZSH does exactly that -> ᛯ cat /tmp/1.sh #!echo ble bla bla bla -n -e -> ᛯ /tmp/1.sh ble bla bla bla -n -e /tmp/1.sh It's the best solution but it will take decades for that to migrate to other shells
- kazinator 4y agoI'm guessing that Zsh is doing this in its fallback strategy for when the exec fails. The exec fails when a shell tries to run an executable file that has no hash bang header, but also in a case like this when the header doesn't give a path that resolves to an interpreter. Without the #!echo, I'm guessing Zsh would have interpreted the file as as Zsh script.
- stonogo 4y agoOf course it has -- and still is, given the /usr/local predilections of the BSDs -- but the /usr merge does nothing to fix it.
- sudobash1 4y agoI have encountered it. When writing a shell script do you write #!/bin/bash or #!/usr/bin/bash at the top? I have had scripts fail when moving to another machine because of having the wrong one. Same thing with /bin/env vs /usr/bin/env It's really not a huge issue for me, but it is at least a minor frustration. (And for people who maintain scripts across different distros / *nixes, I'm sure that it is a larger frustration).
- Quekid5 4y agoAs far as I understand it /usr/bin/env is the only reliable path for "env", i.e. specified by POSIX.
- silverlyra 4y agoI used to work at a startup where one (1) engineer ran FreeBSD. When he joined, he sent a pull request editing the shebangs of all our shell scripts from #!/bin/bash to #!/usr/bin/env bash – because evidently FreeBSD has bash under /usr/bin, but all our systems agreed on where to put env. I still write env bash shebangs to this day, in his honor. Happy to hear there's progress towards ending this archaic separation.
- pdpi 4y agoThat’s probably a good practice anyway. /bin/sh is guaranteed to exist, but there’s no reason to assume bash is installed at all. If it is installed, /usr/bin, /usr/local/bin, /opt/bin are all reasonable locations.
- mbreese 4y agoAnd that’s the way it is in FreeBSD… /bin/sh exists, but it isn’t bash. Instead, bash must be installed by the user, and is thus installed in /usr/bin.
- kevans91 4y agominor nit: /usr/local/bin as it comes from ports
- hvdijk 4y agoEven /bin/sh is not guaranteed to exist, and when it does exist, it is not guranteed to be useful. It is not a location specified by POSIX, and implementations exist that do not place the standard sh utility in /bin. The big one was SunOS/Solaris, where /bin/sh did exist but was the pre-POSIX implementation, and /usr/xpg4/bin/sh existed for when you wanted POSIX sh. Android puts it in /system/bin/sh and not all of them have the compatibility symlink /bin -> /system/bin. There are probably other special cases as well. POSIX explicitly says "Applications should note that the standard PATH to the shell cannot be assumed to be either /bin/sh or /usr/bin/sh, and should be determined by interrogation of the PATH returned by getconf PATH [...]"
- 4y ago
- bmn__ 4y ago