macos 在 OS X 中的路径上的 /usr/local/bin 之前有 /usr/local/bin 有问题吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5364614/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:51:22  来源:igfitidea点击:

Is there a problem with having /usr/local/bin before /usr/bin on the path in OS X?

macospath

提问by Matthew Rankin

By default, OS X 10.6 uses /usr/libexec/path_helperto add the following paths listed in the file /etc/paths:

默认情况下,OS X 10.6 使用/usr/libexec/path_helper添加文件中列出的以下路径/etc/paths

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

This means that /usr/bincomes before /usr/local/binon the path. This results in the version of git installed by Xcode 4in /usr/binto be called instead of the version installed by Homebrew into /usr/local/bin.

这意味着在路径上/usr/bin出现之前/usr/local/bin。这导致Xcode 4in安装git版本/usr/bin被调用,而不是 Homebrew 安装的版本/usr/local/bin

Which leads me to my question, is there a problem with having /usr/local/bincome before /usr/binin the path? Is there a specific reason that Apple defaults to having /usr/bincome before /usr/local/bin?

这让我/usr/local/bin想到了我的问题,之前来过/usr/bin这条路有问题吗?苹果默认有没有特定原因/usr/bin之前来过/usr/local/bin

How to change order of /usr/binand /usr/local/bin

如何更改/usr/bin和的顺序/usr/local/bin

Is it a problem to move /usr/local/binfrom the bottom of the file /etc/pathsto the top? Doing so would impact the path for more than just when I fire up Terminal, since /usr/libexec/path_helpercould be used by other resources (I'm uncertain about this).

/usr/local/bin从文件底部移动/etc/paths到顶部有问题吗?这样做不仅会在我启动终端时影响路径,因为/usr/libexec/path_helper其他资源可能会使用它(我不确定这一点)。

While redundant, it seems safer for me to add /usr/local/binto the path in ~/.bash_profile, which would mean that /usr/local/binwould be on the path twice.

虽然多余,但对我来说添加/usr/local/bin到 中的路径似乎更安全~/.bash_profile,这意味着它/usr/local/bin会在路径上两次。

采纳答案by Forrest Voight

No, and no. They're just weird ... local by definition should override.

不,也没有。他们只是很奇怪......根据定义本地应该覆盖。

回答by Ohgyun Ahn

I had trouble with same problem, and found the link below from googling.

我遇到了同样的问题,并从谷歌搜索中找到了下面的链接。

https://discussions.apple.com/thread/3588837?start=0&tstart=0

https://discussions.apple.com/thread/3588837?start=0&tstart=0

They said that modifying /etc/pathsis not a good idea for security reasons.

他们说出于安全原因修改/etc/paths不是一个好主意。

回答by Complex

I found all the above useful, especially @Ohgyun Ahn'swarning. So I suggest a compromise, which I just implemented:

我发现以上所有内容都很有用,尤其是@Ohgyun Ahn 的警告。所以我建议一个妥协,我刚刚实施:

Edit /etc/pathsor /private/etc/paths(as it is in OS X 10.8) and override git alone. That escapes any security implications (unrelated to git, anyway) while implementing the up-to-date git for use by all programs that actually check the system-wide path.

编辑/etc/paths/private/etc/paths(就像在 OS X 10.8 中一样)并单独覆盖 git。这避免了任何安全隐患(无论如何与 git 无关),同时实现最新的 git 以供所有实际检查系统范围路径的程序使用。

  1. Create a new directory to be used in the override, for example /usr/local/git-override/
  2. Make new symlinks from git-override to homebrew's git programs. Just remake the git symlinks from /usr/local/bin.
  3. add /usr/local/git-overrideto the top of (/private)/etc/paths.
  1. 创建一个要在覆盖中使用的新目录,例如 /usr/local/git-override/
  2. 制作从 git-override 到 homebrew 的 git 程序的新符号链接。只需从 .git 重新制作 git 符号链接即可/usr/local/bin
  3. 添加/usr/local/git-override(/private)/etc/paths.

Hope that's helpful to someone else.

希望这对其他人有帮助。

回答by vmrob

I would just like to add that if you want to keep your PATH clean (no duplicate entries), you can add the following to your .bash_profile to achieve the desired effect:

我只想补充一点,如果你想保持你的 PATH 干净(没有重复的条目),你可以将以下内容添加到你的 .bash_profile 中以达到预期的效果:

# remove /usr/local/bin and /usr/bin
export PATH=`echo ":$PATH:" | sed -e "s#:/usr/local/bin:#:#g" -e "s/^://" -e "s/:$//"`
export PATH=`echo ":$PATH:" | sed -e "s#:/usr/bin:#:#g" -e "s/^://" -e "s/:$//"`
# add /usr/local/bin and /usr/bin in that order
export PATH="/usr/local/bin:/usr/bin:$PATH"

I learned that little trick from http://ntk.me/2013/05/04/path-environment-variable/

我从http://ntk.me/2013/05/04/path-environment-variable/学到了这个小技巧

Edit: Very important! Don't get the order that those are removed wrong! If you do, sedwon't work and you'll be left with /usr/local/bin:/usr/bin:as your PATH!

编辑:非常重要!不要把那些被删除的顺序弄错了!如果你这样做,sed将无法工作,你将被留下/usr/local/bin:/usr/bin:作为你的路径!

It's also worth noting that the others suggesting that this could introduce some security problems are correct. Please be sure to understand the risks involved!

还值得注意的是,其他人认为这可能会引入一些安全问题是正确的。请务必了解所涉及的风险!

回答by 166_MMX

Since modifying the order of /etc/pathsseams to be discouraged for system stability and security ... here my solution which is based on the answer of @vmrob

由于为了/etc/paths系统稳定性和安全性不鼓励修改接缝顺序......这里我的解决方案基于@vmrob的答案

read PATH < <(echo "$PATH" | sed  \
    -e 's/^/:/' -e 's/$/:/'       \
    -e 's_:/usr/local/bin:_:_g'   \
    -e 's_:/usr/local/sbin:_:_g'  \
    -e "s_:/usr/bin:/bin:_:$HOME/bin:/usr/local/bin:/usr/bin:/bin:_"        \
    -e "s_:/usr/sbin:/sbin:_:$HOME/sbin:/usr/local/sbin:/usr/sbin:/sbin:_"  \
    -e 's/^://' -e 's/:$//')
export PATH