问题描述
我知道,为了将目录添加到OS X路径,我应该编辑~/.bashrc
或~/.profile
文件以添加以下内容:
I know that in order to add a directory to my OS X path, I'm supposed to edit my ~/.bashrc
or ~/.profile
file to add something like:
export PATH=<<somepath>>:$PATH
我对bash还是很陌生,并且想知道:是否可以编辑例如我的~/.bash_profile
文件,以便我可以动态地执行此操作,以便可以从命令行将目录永久添加到做类似的事情
I'm pretty new to bash and was wondering: would it be possible to edit, for instance, my ~/.bash_profile
file so that I could do this dynamically, so that from the command line I could permanently add a directory to my path by doing something like
addpath <<somepath>>
相反?
推荐答案
首先在您的主目录中创建一个空文件,该文件将是收集路径中所有新添加内容的地方,所以
First create an empty file in your home directory, this file will be a place to collect all the new additions to your path, so
touch ~/.build_path
接下来,您需要确保在处理~/.bashrc
文件时处理所有新添加的路径,因此请将此行添加到~/.bashrc
文件中:
Next you need to ensure that all your new path additions are processed when your ~/.bashrc
file is processed, so add this line to your ~/.bashrc
file:
source ~/.build_path
最后,将此函数添加到您的~/.bashrc
文件中,此函数立即更改当前的PATH设置,并向~/.build_path
文件添加一个新条目,以便将来的shell可以选择新路径. /p>
Finally, add this function into your ~/.bashrc
file, this function makes an immediate change to the current PATH setting, and adds a new entry to the ~/.build_path
file so that future shells will pick up the new path.
function addpath
{
echo "export PATH=\"$1\":\${PATH}" >> ~/.build_path
export PATH=$1:$PATH
}
应该几乎可以做到.唯一明显的问题是,如果有两个正在运行的Shell,则更改一个Shell中的路径不会导致第二个Shell中的路径被更新,则需要重新启动第二个Shell.
That should pretty much do it. The only obvious problem is that if you have two running shells changing the path in one shell will not cause the path in the second to be updated, you'd need to restart the second shell.
这篇关于如何在Linux/OS X上动态添加到$ PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!