bash 在 OSX 中永久设置 PATH 环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22465332/
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
Setting PATH environment variable in OSX permanently
提问by patti_jane
I have read several answers on how to set environmental variables on OSX as permanently.
我已经阅读了几个关于如何在 OSX 上永久设置环境变量的答案。
First, I tried this, How to permanently set $PATH on Linux/Unix?but I had an error message saying no such file and directory
, so I thought I could try ~/.bash_profile
instead of ~/.profile
but it did not work.
首先,我试过这个,如何在 Linux/Unix 上永久设置 $PATH?但是我有一条错误消息说no such file and directory
,所以我想我可以尝试 ~/.bash_profile
代替,~/.profile
但它没有用。
Second, I found this solution How to set the $PATH as used by applications in os x, which advices to make changes in
其次,我找到了这个解决方案How to set the $PATH as used by applications in os x,它建议在
~/.MacOSX/environment.plist
~/.MacOSX/environment.plist
but again I had no such file and directory
error.
但我又no such file and directory
犯了错误。
I need a way to set these variables such that it won't require to set them again and again each time I open a new terminal session.
我需要一种方法来设置这些变量,这样每次打开新的终端会话时就不需要一次又一次地设置它们。
回答by Nitish
回答by iplus26
I've found that there are some files that may affect the $PATH
variable in macOS (works for me, 10.11 El Capitan), listed below:
我发现有一些文件可能会影响$PATH
macOS 中的变量(适用于我,10.11 El Capitan),如下所列:
As the top voted answer said,
vi /etc/paths
, which is recommended from my point of view.Also don't forget the
/etc/paths.d
directory, which contains files may affect the$PATH
variable, set thegit
andmono-command
path in my case. You canls -l /etc/paths.d
to list items andrm /etc/paths.d/path_you_dislike
to remove items.If you're using a "bash" environment (the default
Terminal.app
, for example), you should check out~/.bash_profile
or~/.bashrc
. There may be not that file yet, but these two files have effects on the$PATH
.If you're using a "zsh" environment (Oh-My-Zsh, for example), you should check out
~./zshrc
instead of~/.bash*
thing.
正如投票最高的答案所说,
vi /etc/paths
从我的角度推荐。另外不要忘记
/etc/paths.d
包含可能影响$PATH
变量的文件的目录,在我的情况下设置git
和mono-command
路径。您可以ls -l /etc/paths.d
列出项目和rm /etc/paths.d/path_you_dislike
删除项目。如果您使用的是“bash”环境(
Terminal.app
例如,默认的),您应该查看~/.bash_profile
或~/.bashrc
。可能还没有那个文件,但这两个文件对$PATH
.如果您使用的是“zsh”环境(例如Oh-My-Zsh),则应该检查
~./zshrc
而不是~/.bash*
事物。
And don't forget to restart all the terminal windows, then echo $PATH
. The $PATH
string will be PATH_SET_IN_3&4:PATH_SET_IN_1:PATH_SET_IN_2
.
并且不要忘记重新启动所有终端窗口,然后echo $PATH
. 该$PATH
字符串会PATH_SET_IN_3&4:PATH_SET_IN_1:PATH_SET_IN_2
。
Noticed that the first two ways (/etc/paths
and /etc/path.d
) is in /
directory which will affect all the accounts in your computer while the last two ways (~/.bash*
or ~/.zsh*
) is in ~/
directory (aka, /Users/yourusername/
) which will only affect your account settings.
请注意,前两种方式 (/etc/paths
和/etc/path.d
) 位于/
目录中,会影响您计算机中的所有帐户,而后两种方式 (~/.bash*
或~/.zsh*
) 位于~/
目录中 ( 又名/Users/yourusername/
) ,只会影响您的帐户设置。
回答by omoman
You could also add this
你也可以添加这个
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
to ~/.bash_profile
, then create ~/.bashrc
where you can just add more paths to PATH. An example with .
to ~/.bash_profile
,然后创建~/.bashrc
可以向 PATH 添加更多路径的位置。一个例子.
export PATH=$PATH:.
回答by Tiago
You can open any of the following files:
您可以打开以下任何文件:
/etc/profile
~/.bash_profile
~/.bash_login (if .bash_profile does not exist)
~/.profile (if .bash_login does not exist)
And add:
并添加:
export PATH="$PATH:your/new/path/here"
回答by Rishabh Mishra
For a new path to be added to PATH environment variable in MacOS just create a new file under /etc/paths.d
directory and add write path to be set in the file. Restart the terminal. You can check with echo $PATH
at the prompt to confirm if the path was added to the environment variable.
在 MacOS 中要添加到 PATH 环境变量的新路径只需在/etc/paths.d
目录下创建一个新文件并添加要在文件中设置的写入路径。重启终端。您可以echo $PATH
在提示符处检查以确认路径是否已添加到环境变量中。
For example: to add a new path /usr/local/sbin
to the PATH
variable:
例如:添加一个新的路径/usr/local/sbin
到PATH
变量:
cd /etc/paths.d
sudo vi newfile
Add the path to the newfile
and save it.
添加路径newfile
并保存。
Restart the terminal and type echo $PATH
to confirm
重启终端并输入echo $PATH
确认
回答by IgnitedMind
i tried the first method and gone through reference page,well executed path setting.But it did not shoe the path set when i echo$PATH
我尝试了第一种方法并浏览了参考页面,路径设置执行得很好。但是当我 echo$PATH 时它没有设置路径
回答by Ram Krishna
For setting up path in Mac two methods can be followed.
在 Mac 中设置路径有两种方法。
- Creating a file for variable name and paste the path there under /etc/paths.d and source the file to profile_bashrc.
Export path variable in
~/.profile_bashrc
asexport VARIABLE_NAME = $(PATH_VALUE)
- 为变量名创建一个文件并将路径粘贴到 /etc/paths.d 下,并将文件来源到 profile_bashrc。
将路径变量导出
~/.profile_bashrc
为导出变量名 = $(PATH_VALUE)
AND source the the path. Its simple and stable.
并提供路径。它简单而稳定。
You can set any path variable
by Mac terminal
or in linux
also.
您也可以设置任何path variable
byMac terminal
或 in linux
。