bash $PATH 中 /usr/bin 和 /usr/local/bin 等的顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34984870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Order of /usr/bin and /usr/local/bin and more in $PATH
提问by Bruce
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.
在我的 Mac 上,我经常使用 bash。对于我的环境设置,我像往常一样添加/usr/bin
并/usr/local/bin
进入$PATH
。
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/bin
和/usr/local/bin
是的,我很好奇这之前,另一个应该去,按照惯例?有什么具体原因吗?
Similar is for /usr/lib
and /usr/local/lib
-- hopefully the answer is the same or similar.
相似是为了/usr/lib
和/usr/local/lib
——希望答案是相同或相似的。
a bit more -- Just an extension of the original question, how would you order the following in $PATH following the convention and why?
多一点 - 只是原始问题的扩展,您将如何按照约定在 $PATH 中订购以下内容,为什么?
/bin
/sbin
/usr/bin
/usr/sbin
/usr/local/bin
/usr/local/sbin
/opt/local/bin
/opt/local/sbin
回答by Asaph
/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 Homebrewpackage manager tool.
/usr/bin
是操作系统提供的二进制文件所在的位置。/usr/local/bin
是用户提供的二进制文件所在的位置。当您在命令行上键入命令的名称时,shell 会在$PATH
环境变量中包含的路径中按顺序搜索该命令。一个常见的模式是有/usr/local/bin
普瑞森/usr/bin
在$PATH
。这允许您安装二进制文件的替代版本,并让它们优雅地“覆盖”操作系统提供的二进制文件。并且操作系统更新不会破坏您的用户安装的软件包。流行的Homebrew包管理器工具在 OSX 中特别使用了这种模式。
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 提供 git 版本 2.5.4(在 中/usr/bin
)。如果你想要更新的版本,你可以使用 Homebrew 安装 git 版本 2.7.0(进入/usr/local/bin
)。由于/usr/local/bin
出现/usr/bin
在$PATH
环境变量之前,因此当您git
在 shell 中发出命令时,将使用较新的 Homebrew 版本。
On fresh new Mac running OSX El Capitan (I happen to have one), the /etc/paths
file contains the following:
在运行 OSX El Capitan 的全新 Mac 上(我碰巧有一台),该/etc/paths
文件包含以下内容:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
which produces the following $PATH
environment variable:
它产生以下$PATH
环境变量:
/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.
这与自制软件兼容。我建议坚持使用这个 OSX 默认值。如果你真的想包括/usr/local/sbin
(在你上面的问题中列出),我会把它放在前面/usr/sbin
,原因与上面类似。至于/opt/local/bin
and /opt/local/sbin
,我个人没有发现需要将它们添加到路径中,但似乎它们可能与它们的/usr/local
类似物处于相似的位置,因为/opt
传统上包含用户安装的二进制文件。
Note: The same explanation applies to /usr/lib
vs. /usr/local/lib
.
注意:相同的解释适用于/usr/lib
vs. /usr/local/lib
.