在 bash 中清理 $PATH

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

cleaning up $PATH in bash

bashpath

提问by mbarrows

My path has a lot of entries that were added long ago by scripts. They are not in my .bashrc, .bash_profile, or .bash_login.

我的路径有很多条目是很久以前由脚本添加的。它们不在我的.bashrc, .bash_profile, 或 中.bash_login

I'm worried that resetting my path in .bashrcwill have undesirable long-term results. Is there a way to find where things have been added to my path and remove them manually? Are things always added by file or is path cached somewhere? If the latter, is it easy to clean that up?

我担心重新开始我的道路.bashrc会产生不良的长期结果。有没有办法找到添加到我的路径中的内容并手动删除它们?东西总是由文件添加还是路径缓存在某处?如果是后者,清理起来容易吗?

回答by gavenkoa

The easiest way to find whomodified your PATHis to run:

查找修改了您的PATH的最简单方法是运行:

  $ bash --login -i -xv 2>&1 | grep ' \. '

For example I got:

例如我得到:

+ . /etc/profile.d/bash_completion.sh
        . /etc/bash_completion
++ . /etc/bash_completion
+++ . /etc/bash_completion.d/abook
+++ . /etc/bash_completion.d/ant
+ . /etc/profile.d/lapack0.sh
+ . /etc/profile.d/openssl.sh
+ . /etc/profile.d/qt3-devel.sh
+ . /etc/profile.d/tetex-profile.sh
+ . /etc/profile.d/xinit.sh
+ . /etc/bash.bashrc

...

...

回答by Jens

You shouldn't let some random sysadmin decide what's in your PATH anyway, you should set it to the PATH you need. You begin with

无论如何,您不应该让一些随机的系统管理员决定您的 PATH 中的内容,您应该将其设置为您需要的 PATH。你开始

# POSIX way of getting the system's PATH to POSIX tools:
PATH=$(getconf PATH)   # Or /usr/bin/getconf PATH.

followed by whatever you require in addition, e.g.

其次是您额外需要的任何内容,例如

PATH="$PATH:/usr/local/bin"
PATH="$PATH:/usr/local/sbin"
PATH="$PATH:$HOME/bin"

and put this in your shell's .profileor equivalent. Note that you do notwant .or world-writable directories in your PATH for security reasons.

并将其放入您的 shell.profile或等效项中。请注意,出于安全原因,您希望.PATH 中的目录或世界可写目录。

回答by Jonathan Leffler

You're always at liberty to look at the directory contents for each component of $PATHand decide whether you use the programs therein. If you don't use the programs, the chances are, you won't be hurt by removing the directory from $PATH. If the directory doesn't exist, then you can completely safely remove it.

您始终可以自由查看每个组件的目录内容$PATH并决定是否使用其中的程序。如果您不使用这些程序,很可能不会因为从 $PATH 中删除目录而受到伤害。如果该目录不存在,那么您可以完全安全地将其删除。

It is puzzling that the directories show up in your profile and related files. You should check for ~/.profiletoo. You should also look at material like /etc/profile.

令人费解的是,这些目录显示在您的个人资料和相关文件中。你也应该检查一下~/.profile。您还应该查看像/etc/profile.

Personally, I consider I am in charge of my PATH. I set it from scratch according to my rules, picking the directories I need. You're not obliged to accept what the system admins set for you, though you should not idly remove PATH components that they've added. But their views on what's desirable may be different from yours.

就个人而言,我认为我负责我的 PATH。我根据我的规则从头开始设置它,选择我需要的目录。您没有义务接受系统管理员为您设置的内容,但您不应随意删除他们添加的 PATH 组件。但他们对什么是可取的看法可能与你不同。

The only long-term undesirable effect might be that some program you use stops working because it relied on something from the old version of $PATH. So, keep a record of what you had before you started messing with PATH - but don't be afraid to adjust PATH to suit yourself.

唯一的长期不良影响可能是您使用的某些程序停止工作,因为它依赖于旧版本的 $PATH 中的某些内容。因此,请在开始使用 PATH 之前记录下您拥有的内容 - 但不要害怕调整 PATH 以适合自己。

回答by Manny D

Check your /etc/profilefile, and, depending on your OS version, /etc/profile.d/directory.

检查您的/etc/profile文件,并根据您的操作系统版本检查/etc/profile.d/目录。