News for hackers. Stuff that matters.
Re:Which is not Posix by mike256 on Monday November 01, 2021 @02:30PM (#29070870)
Linux from Scratch has a very tiny implementation suitable for all systems that have bash installed:

  #!/bin/bash
  type -pa "$@" | head -n 1 ; exit ${PIPESTATUS[0]}
Just save it as which, chmod +x and add to path.

https://www.linuxfromscratch.org/blfs/view/svn/general/which...

Re:Which is not Posix by Athas on Monday November 01, 2021 @03:02PM (#29071352)
Not all systems have bash as /bin/bash. A shebang line of '#!/usr/bin/env bash' would be even more portable.
Re:Which is not Posix by IggleSniggle on Monday November 01, 2021 @03:52PM (#29072097)
I like bash 4+ for associative arrays, and use it on macOS. But macOS is still v3 by default (for /bin/bash).

I think `/usr/bin/env bash` is a great shebang, for the record, but I'm not totally convinced that it's more portable than `/bin/bash`. Can you explain what makes it actually more portable? In absence of that, you might actually expect `/bin/bash` to be more portable, as at least you can more likely make assumptions based on the OS.

Re:Which is not Posix by pxc on Monday November 01, 2021 @04:27PM (#29072598)
The install path of the `env` executable varies less across operating systems than the install path of the `bash` executable. Some `bash` installation paths by operating system:

  typical GNU/Linux: /bin/bash
  FreeBSD, NetBSD, OpenBSD: /usr/local/bin/bash
  Solaris, Illumos, OpenIndiana, SmartOS, etc.: /usr/bin/bash
  macOS: /bin/bash
  NixOS, GuixSD†: /run/current-system/sw/bin/bash
but all of these operating systems have a standards-compliant `env` installed at `/usr/bin/env`.

— †: GuixSD may have slightly tweaked the structure of their profiles; I'm not sure if it's at `.../sw/bin` or it's something else after the ...

Re:Which is not Posix by lvass on Monday November 01, 2021 @03:56PM (#29072159)
>Can you explain what makes it actually more portable?

POSIX. The NixOS machine I'm typing this on has /usr/bin/env but not /bin/bash.

Re:Which is not Posix by gnubison on Monday November 01, 2021 @05:45PM (#29073575)
POSIX does not specify /usr/bin/env, but the location is pretty much universal anyways.
Re:Which is not Posix by IggleSniggle on Monday November 01, 2021 @04:34PM (#29072683)
thanks
Re:Which is not Posix by richardwhiuk on Monday November 01, 2021 @03:16PM (#29071574)
Not all systems have env has /usr/bin/env.
Re:Which is not Posix by pxc on Monday November 01, 2021 @04:14PM (#29072413)
The presence of the env command is required by POSIX, and /usr/bin/env is the closest thing we have to a standard path for it.
Re:Which is not Posix by richardwhiuk on Thursday November 11, 2021 @06:25AM (#29186812)
/bin/bash is the closest thing we to a standard path for bash.