如何修改PATH以便在每个终端会话中都可以进行更改

我想添加一个目录来搜索我的搜索路径。 我知道我必须修改PATH环境变量。 但是,我希望更改是永久性的,以便它始终生效,对于我打开的每个终端(bash)窗口。

https://help.ubuntu.com/community/EnvironmentVariables中存在令人困惑且可能存在冲突信息的过载

我正在使用Ubuntu 10.04。 假设我想将/usr/local/foo到我的PATH 。 我应该修改哪个文件( .bashrc.profile.bash_login等…)以及新行应该是什么样的?

以下命令添加当前路径的路径:

 export PATH=$PATH:/my/custom/path 

如果您希望您的设置每次都执行此命令,则可以在许多地方放置它。 登录时,将按以下顺序执行以下脚本:

 /etc/profile (which starts by loading everything in /etc/profile.d) ~/.profile (which starts by loading ~/.bashrc if you are running bash) 

笔记

  • 只有~/.bash_profile~/.bash_login不存在时才加载~/.profile 。 否则,至少bash,将加载它们。 建议使用.profile而不是bash特定脚本。 因此,如果在这些尝试中您创建了.bash_login请立即将其删除。

  • 只有在运行交互式会话时才会加载~/.bashrc 。 (带有提示的东西,你可以在其中输入内容)。

  • 每次打开一个新终端时, 都会一次又一次地加载~/.bashrc 。 因此,gnome-terminal中的新选项卡,新的虚拟终端等等。因此,即使您不再次登录,每次打开新shell时都会加载.bashrc (从而重置其环境)。

  • 像byobu这样的东西应该真的进入.profile ,(否则它将无法工作;-)

  • 如果你希望它们在交互式会话之外工作,那么路径之类的东西应该进入.profile 。 (比如当你在GNOME中按Alt + F2时)

我通过修改~/.profile

看起来像添加〜/ bin到我的路径是个坏例子,因为〜/ .profile中已经有代码自动执行,如果目录存在的话。

要将usr / local / foo目录添加到我前进的每个会话的路径中,我在.profile的末尾添加/编辑以下行:

 export PATH=$PATH:/usr/local/foo 

但是,为了使这个生效,我需要注销并重新登录(只需关闭终端窗口并打开一个新窗口不起作用)。

要重新加载.profile并在没有注销/登录的情况下获取更改效果,请运行:

 source ~/.profile 

您可以添加/etc/environment的路径,但要注意没有shell扩展可以工作; 变量将按字面设置您输入的字符。

您可以修改$HOME目录中的.bashrc文件。

在此文件的最后,添加以下行:

 export PATH="$HOME/directory_to_include_in_path/:$PATH" 

您还可以在$HOME目录中修改.profile文件,包括以下行:

 PATH="$HOME/directory_to_include_in_path/:$PATH" 

这对我有用。

 Going through the basics, I will suggest the following steps: 1. It's recommended to set environment variables in /etc/environment 2. Open the file as superuser in an editor as it's a read only file eg gedit: gksu gedit /etc/environment 3. System will need password to open it in editable mode. Enter your superuser password and get file opened in a new gedit window. 4. Add new line at the end of file with export PATH=$PATH:/usr/local/foo 5. Save and close the window. It will get command back to terminal. 6. Refresh the environment by running the following command: . /etc/environment 7. You may check by executing the following command: echo $PATH