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 do know what /usr/bin
and /usr/local/bin
are about, I am curious which should go before another, by convention? And is there a specific reason for that?
Similar is for /usr/lib
and /usr/local/lib
-- hopefully the answer is the same or similar.
a bit more -- Just an extension of the original question, how would you order the following in $PATH following the convention and why?
/bin
/sbin
/usr/bin
/usr/sbin
/usr/local/bin
/usr/local/sbin
/opt/local/bin
/opt/local/sbin
-- /usr/bin This is the primary directory for executable programs. Most programs executed by normal users which are not needed for booting or for repairing the system and which are not installed locally should be -- /usr/local/bin Binaries for programs local to the site.
In traditional Unix systems, /usr usually contains files that come with the system distribution, and the /usr/local tree is free for the local administrator to manage. The only really hard and fast rule is that Unix distributions should not touch /usr/local , except perhaps to create the basic directories within it.
/bin contains executable files that are part of the core operating system. These files need to be accessible before /usr gets mounted. (for instance, the mount command is in /bin/mount ). /usr/bin contains executable files that are not part of the core operating system.
/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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With