bash 如何更改 $PATH 的顺序?

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

How do I change the order of $PATH?

bashpath

提问by pill45

echo $PATHgives me

echo $PATH给我

/Library/Frameworks/Python.framework/Versions/3.4/bin:/Applications/Sublime Text 2.app/Contents/SharedSupport/bin:/Users/pathreskoo/anaconda/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin

but when I want to change the order of /usr/local/binto the front of /Library/Frameworks/Python.framework/Versions/3.4/bin, I type

但是当我想将 的顺序更改/usr/local/bin为 的前面时/Library/Frameworks/Python.framework/Versions/3.4/bin,我输入

sudo emacs /etc/paths

I only get

我只得到

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

How can I insert /usr/local/binin front of my PATH?

我怎样才能在我/usr/local/bin的前面插入PATH

回答by Brad Peabody

You can set your PATHin the file .bash_profile, which is in your home directory.

您可以在您的主目录PATH中的文件中设置您的.bash_profile

More specifically, you can simply add the following line to the end of that file

更具体地说,您只需将以下行添加到该文件的末尾

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

This results in /usr/local/binbeing prepended to the existing PATH. In other words, the folder /usr/local/binis inserted in front of your PATH, and so it would have the highest priority. You can also append a folder to your path by doing

这导致/usr/local/bin被添加到现有的PATH. 换句话说,文件夹/usr/local/bin插入在您的 前面PATH,因此它具有最高优先级。您还可以通过执行将文件夹附加到您的路径

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

In general, you can set the order of the folders or files that you export in a similar way as the following:

一般情况下,您可以按照如下类似的方式设置您导出的文件夹或文件的顺序:

export PATH=/usr/local/bin:/Applications/Sublime Text 2.app/Contents/SharedSupport/bin:/Users/pathreskoo/anaconda/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin

Note: this is not the only place you can set the PATH, but it is a common one.

注意这不是唯一可以设置 的地方PATH,但它是一个常见的地方。

回答by u7137467

Your $PATH normally overridden by the initiation part of your shell. Normally follows the system-wide profile (/etc/profile), then user-side profile (if you use bash .profile, .bash_profile, .bashrc) and any source command in these files. The overridden command mainly in .bashrc

您的 $PATH 通常会被 shell 的启动部分覆盖。通常遵循系统范围的配置文件 (/etc/profile),然后是用户端配置文件(如果您使用 bash .profile、.bash_profile、.bashrc)以及这些文件中的任何源命令。主要在 .bashrc 中的覆盖命令

Edit you .bashrc file and find $PATH, you may find the export command and delete the path you do not want. export $PATH=/usr/local/bin:$PATH override the command user-wide.

编辑你的 .bashrc 文件并找到 $PATH,你可能会找到 export 命令并删除你不想要的路径。export $PATH=/usr/local/bin:$PATH 覆盖用户范围的命令。