Order of /usr/bin and /usr/local/bin and more in $PATH

前端 未结 1 1113
無奈伤痛
無奈伤痛 2021-01-31 05:50

On my Mac I used bash a lot. For my environment settings I added /usr/bin and /usr/local/bin into $PATH as I normally did.

While I

相关标签:
1条回答
  • 2021-01-31 06:49

    /usr/bin is where binaries supplied by the OS go. /usr/local/bin is where user supplied binaries go. When you type the name of a command on the command line, the shell searches for said command in the paths contained in the $PATH environment variable in order. A common pattern is to have /usr/local/bin precede /usr/bin in $PATH. This allows you to install alternate versions of binaries and have them gracefully "override" the binaries provided by the OS. And OS updates won't clobber your user installed packages. This pattern is notably used in OSX by the popular Homebrew package manager tool.

    For instance, at the time of this writing, OSX El Capitan supplies git version 2.5.4 (in /usr/bin). If you want a newer version, you can use Homebrew to install git version 2.7.0 (into /usr/local/bin). Since /usr/local/bin comes before /usr/bin in the $PATH environment variable, when you issue the command git in a shell, the newer Homebrew version will be used.

    On fresh new Mac running OSX El Capitan (I happen to have one), the /etc/paths file contains the following:

    /usr/local/bin
    /usr/bin
    /bin
    /usr/sbin
    /sbin
    

    which produces the following $PATH environment variable:

    /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
    

    which is compatible with homebrew. I recommend sticking with this OSX default. If you really want to include /usr/local/sbin (listed in your question above), I would put it just before /usr/sbin for similar reasons to above. As for /opt/local/bin and /opt/local/sbin, I haven't personally found the need to add them to the path but it seems they might go in a similar spot as their /usr/local analogs, since /opt traditionally contains user installed binaries.

    Note: The same explanation applies to /usr/lib vs. /usr/local/lib.

    0 讨论(0)
提交回复
热议问题