I recently noticed that many scripts are using /usr/bin/env in their shebang. I have seen that mainly using Bash and Python, but thus far never in conjunction with POSIX sh (ash, dash,...).
I wonder why, and if my, meant-to-be highly portable, POSIX shell scripts might benefit from the env approach?
Is there a general concensus on whether to use:
standard:
#!/bin/shenvironment:
#!/usr/bin/env sh
Let me stress this enough:
I never have seen this with sh.
/bin/bashis an antiquated POC, but one can get newer bash via homebrew. So I use#!/usr/bin/env bashif write a script for both my MBP and my Linux PC. But if I'm going to stick to POSIX, then/bin/shis fine for both; I don't see much of a use case except in some outdated system where/bin/shmight not be a POSIX shell, in which case I doubt/usr/bin/envwill work either. – muru Feb 27 '20 at 03:51/bin/shand/usr/bin/env. Some obscure and mostly retired Unix variants had/bin/envwithout having/usr/bin/env, but you're unlikely to encounter them. Modern systems have/usr/bin/envprecisely because of its widespread use in shebangs./usr/bin/envis something you can count on." – muru Feb 27 '20 at 03:52/bin/shand/usr/bin/env" except that neither path is standard and the most used unix variant ever (like >99% of existing installations) doesn't have either of them. – Feb 27 '20 at 04:41/bin/shor shebang-ed scripts. Both are non-standard extensions. – Feb 27 '20 at 04:48/bin/shand an actual POSIX sh somewhere else? – muru Feb 27 '20 at 07:02bashcompiled with default parameters is not POSIX compliant, this is why Apple offers a specialbashversion in/bin/shthat e.g. treatsechocorrectly. BTW:/bin/shis not part of POSIX. POSIX rather requires you to do:export PATH=$(getconf PATH)followed byshto get a POSIX shell. – schily Feb 27 '20 at 15:46#!/bin/sh, because the POSIX standard mandates that/bin/shis the standard (Bourne) shell. – vonbrand Feb 28 '20 at 16:26/bin/sh?! First off, that won't be able to get certified as Unix... And sorry, no Linux distribution has been certified as "Unix". Too much hassle for little gain. – vonbrand Feb 28 '20 at 16:28/bin/sh. 2. Except for some obscure Chinese SUSE (or Centos?) fork(s) (iirc), no Linux system has ever been "certified" as Unix. – Feb 28 '20 at 16:31/bin/sh. But, for the record, it does specify in the APPLICATION USAGE odshthat the utility sh must be found withcommand -v sh. – Jun 11 '20 at 20:42