问题描述
在Mac上,我经常使用bash.对于我的环境设置,我像往常一样在$PATH
中添加了/usr/bin
和/usr/local/bin
.
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.
虽然我确实知道/usr/bin
和/usr/local/bin
是关于什么的,但我很好奇,按照惯例,哪个应该在另一个之前?并有特定的原因吗?
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?
与/usr/lib
和/usr/local/lib
类似-希望答案是相同或相似的.
Similar is for /usr/lib
and /usr/local/lib
-- hopefully the answer is the same or similar.
多一点-只是原始问题的扩展,您如何按照约定在$ PATH中排序以下内容?为什么?
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
是操作系统提供的二进制文件所在的位置. /usr/local/bin
是用户提供的二进制文件所在的位置.在命令行上键入命令名称时,shell在$PATH
环境变量 中包含的路径中搜索该命令.一个常见的模式是在$PATH
中将/usr/local/bin
放在/usr/bin
之前.这样,您就可以安装二进制的备用版本,并使其优雅地覆盖"操作系统提供的二进制文件.而且操作系统更新不会破坏您用户安装的软件包.流行的 Homebrew 程序包管理器工具在OSX中特别使用了这种模式.
/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.
例如,在撰写本文时,OSX El Capitan提供git版本2.5.4(在/usr/bin
中).如果您想要更新的版本,则可以使用Homebrew将git版本2.7.0(安装到/usr/local/bin
中)安装.由于/usr/local/bin
在$PATH
环境变量中的/usr/bin
之前,因此在外壳程序中发出命令git
时,将使用较新的Homebrew版本.
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.
在运行OSX El Capitan(我碰巧有一个)的全新Mac上,/etc/paths
文件包含以下内容:
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
会产生以下$PATH
环境变量:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
与自制软件兼容.我建议坚持使用此OSX默认设置.如果您确实要包括/usr/local/sbin
(在您的问题中列出),出于与上述类似的原因,我将其放在/usr/sbin
之前.至于/opt/local/bin
和/opt/local/sbin
,我个人还没有发现需要将它们添加到路径中,但是似乎它们可能与/usr/local
类似物相似,因为/opt
传统上包含用户安装的二进制文件
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.
注意:/usr/lib
与/usr/local/lib
的解释相同.
这篇关于$ usr中/usr/bin和/usr/local/bin的顺序以及更多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!