What's the difference of using #!/usr/bin/env or #!/bin/env in shebang?

前端 未结 4 864
野的像风
野的像风 2021-02-19 07:19

Will there be any difference or it\'s just a personal choice?

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-19 08:18

    Historically, UNIX had 2 bunches of binaries:

    • / filesystem mounted early on boot.
    • /usr might have been mounted later, possibly running scripts and programs from / to arrange the mount. Example: some sites saved space by mounting /usr from network, but you need to get on the network first. Example: it's a large local filesystem, but if it gets damaged, you want tools like fsck to try fixing it.

    Thus, /bin and /sbin (using /lib), had to contain a minimal system including at least a shell at /bin/sh, scripting essentials like /bin/echo, /bin/test etc., system tools like /bin/mount or /sbin/mount, and /bin/fsck...

    Thus in different unixes, almost any program might have been:

    • in /usr/bin/ but NOT /bin
    • in /bin but NOT /usr/bin
    • in both, same as symlink
    • in both but different! E.g. some systems played with /bin/sh being a very minimal shell (e.g. dash) for faster startup, but symlinking /usr/bin/sh -> /usr/bin/bash (iirc, invoking bash as "sh" puts it into some posix mode, but it's still a different more powerful shell).

    Thus the trick of using env to write portable scripts — env just happens to do a PATH lookup. (It's also useful in other places that don't do PATH lookups, e.g. docker exec.)

    What changed

    These were valid use cases but modern Linux followed a similar argument of "need small userspace to mount / recover main userspace" for / itself too! The pivot syscall and initrd were introduced, and tooling grew to copy the parts you need into it.

    /usr unification

    Now, / vs /usr arguably lost its purpose. Having both on one filesystem and symlinking was doable for everybody in principle, though some particular setups would break and would have to change...

    See https://lwn.net/Articles/483921/ from 2012 for overview of this "/usr unification" idea. For example Fedora completed it: https://fedoraproject.org/wiki/Features/UsrMove. Many other distros haven't, or are still debating it, or ironing out some kinds to break less users. For example see debian's preparation: https://wiki.debian.org/UsrMerge.

提交回复
热议问题