bash 在 Yosemite 中设置环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28371285/
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 environment variables in Yosemite
提问by Carlos Bribiescas
What is the proper way to modify environment variables like PATH in Yosemite?
在优胜美地修改 PATH 等环境变量的正确方法是什么?
This is this question Setting environment variables in OS X?but specifically for yosemite since it doesn't work anymore.
这是这个问题在 OS X 中设置环境变量?但专门针对优胜美地,因为它不再起作用了。
回答by domino
Have you tried editing ~/.bash_profile
?
你试过编辑~/.bash_profile
吗?
Adding a line like this to bash_profile
ought to do it:
添加这样的一行bash_profile
应该这样做:
export PATH=/usr/local/bin:$PATH
导出路径=/usr/local/bin:$PATH
回答by David W.
What shell are you using? I'm assuming you're using the default Bash shell. There's also Csh, Ksh, and Zsh.
你用的是什么壳?我假设您使用的是默认的 Bash shell。还有 Csh、Ksh 和 Zsh。
The Terminal.app
on Mac OS X by default starts a new loginshell each time a window is open. This means that $HOME/.bash_profile
or $HOME/profile
is always executed when you open a new terminal window. You can set particular defaults in here. For example, I set PS1
and set -o vi
.
在Terminal.app
Mac OS X上默认启动一个新的登录每次窗口打开外壳。这意味着$HOME/.bash_profile
or$HOME/profile
总是在您打开新的终端窗口时执行。您可以在此处设置特定的默认值。例如,我设置了PS1
和set -o vi
。
NOTE: This may not be the case if you're using other Terminal apps like
xterm
. These open new terminal windows as just new shells. This means that you may not see the changes made in.bash_profile
until you log out and log back in.You can try editing
$HOME/.bashrc
and see if that helps.What about other shells?
If you're using Kornshell (
ksh
), you need to edit the$HOME/profile
and not$HOME/.bash_profile
. If you're using Zshell (zsh
), you're on your own. It's too wacky to describe here. Read the manpage forzsh
and search forZDOTDIR
.
注意:如果您使用其他终端应用程序(如
xterm
. 这些打开新的终端窗口只是新的外壳。这意味着在.bash_profile
您注销并重新登录之前,您可能看不到所做的更改。您可以尝试编辑
$HOME/.bashrc
,看看是否有帮助。其他贝壳呢?
如果您使用的是 Kornshell (
ksh
),则需要编辑$HOME/profile
而不是$HOME/.bash_profile
。如果您使用的是 Zshell (zsh
),那么您就靠自己了。在这里描述太古怪了。阅读联机帮助页zsh
并搜索ZDOTDIR
.
When you run a shell script, the $HOME/.bashrc
is executed. Most people put something like this in their .bash_profile
, so their .bashrc
settings are included in a new terminal window:
当您运行 shell 脚本时,将$HOME/.bashrc
执行该脚本。大多数人在他们的 中放了这样的东西.bash_profile
,所以他们的.bashrc
设置包含在一个新的终端窗口中:
[[ -x $HOME/.bashrc ]] && source "$HOME/.bashrc"
Some people set things they want to be set when they run a shell script, for example export $PS4="\$LINENO> "
.
有些人设置了他们在运行 shell 脚本时想要设置的东西,例如export $PS4="\$LINENO> "
.
The $PATH
is a bit different. You can set it in .bash_profile
(I would not set it in .bashrc
), But, Mac OS X has an automated why on how systemwide paths are set. A file called /etc/paths
is used to set the default path for all users using either Bash or Kornshell via the /usr/libexec/path_helper
program.
该$PATH
有点不同。您可以将其设置为.bash_profile
(我不会将其设置为.bashrc
),但是,Mac OS X 具有自动设置系统范围路径的原因。一个名为的文件/etc/paths
用于通过/usr/libexec/path_helper
程序为所有使用 Bash 或 Kornshell 的用户设置默认路径。
On my Mac, I set my $PATH
to:
在我的 Mac 上,我设置$PATH
为:
/usr/local/bin:/usr/share/bin:/bin:/usr/bin:/usr/sbin:/sbin:$HOME/bin
When I install programs, I usually install them under /opt
when possible. Then, I link their binaries (where ever they're placed) to /usr/local/bin
. This way, I don't have to keep building my PATH. Plus, it allows me to override system defaults. For example, /usr/bin/git
is at 1.9.3. while my installed /usr/local/bin/git
is at version 2.2.1.
当我安装程序时,我通常会/opt
在可能的情况下安装它们。然后,我将它们的二进制文件(它们放置的位置)链接到/usr/local/bin
. 这样,我就不必继续构建我的 PATH。另外,它允许我覆盖系统默认值。例如,/usr/bin/git
在 1.9.3。而我安装/usr/local/bin/git
的版本是 2.2.1。
One thing you should not do is modify /etc/profile
because changes there may be replaced on OS X upgrades.
您不应该做的一件事是修改,/etc/profile
因为 OS X 升级可能会替换那里的更改。
回答by pbw
The problem is not with environment variables set and accessed from within /bin/bash or /bin/sh, but with envars that should be set for programs NOT executed from the shell; i.e. normal apps executed from the dock or Finder.
问题不在于从 /bin/bash 或 /bin/sh 中设置和访问的环境变量,而在于应为不是从 shell 执行的程序设置的环境变量;即从 Dock 或 Finder 执行的普通应用程序。
After finally getting things right with launchctl in Mavericks, Apple is in the process of changing things again. The useful subcommands of launchctl are now labelled as "legacy subcommands", some of which are no longer supported. That leaves a question mark over the others.
在 Mavericks 中使用 launchctl 终于把事情做好后,Apple 正在再次改变事情。launchctl 的有用子命令现在被标记为"legacy subcommands",其中一些不再受支持。这给其他人留下了一个问号。
In any case, the most important subcommands are still available for now.
无论如何,最重要的子命令现在仍然可用。
launchctl allows for the setting of the overall environment in which user processes execute. It appears that the overall user environment is inherited by all Terminal processes; i.e. all setenv variables are exported. It's a bit tricky to confirm that. In any case, you will still need your .profile and .bashrc to define functions and aliases, which are not supported by launchctl.
launchctl 允许设置用户进程执行的整体环境。看来整个用户环境被所有终端进程继承了;即导出所有 setenv 变量。确认这一点有点棘手。在任何情况下,您仍然需要您的 .profile 和 .bashrc 来定义函数和别名,而 launchctl 不支持这些。
I go to some lengths to ensure that all of my launchctl vars are also defined in my profile. This enables me to set up the same environment on remote or VM linux systems, with a few minor tweaks.
我竭尽全力确保我的所有 launchctl 变量也在我的配置文件中定义。这使我能够在远程或 VM linux 系统上设置相同的环境,只需稍作调整。
Al of my setup is described in this blog post.
这篇博文中描述了我的所有设置。
回答by positivecrux
following solution worked for me.
以下解决方案对我有用。
- Open Terminal
- Click on Terminal Menu at right upper corner.
- click on Preferences
- Click on General
- Change Shell open with to command and put /bin/bash in text box.
- 打开终端
- 单击右上角的终端菜单。
- 单击首选项
- 点击通用
- 将 Shell open with 更改为命令并将 /bin/bash 放入文本框中。
Now whatever configuration you do in ~/.bash_profile takes effect. Previously you were not using bash(were using ksh) that is why it was not reading .bash_profile.
现在你在 ~/.bash_profile 中所做的任何配置都会生效。以前您没有使用 bash(正在使用 ksh),这就是它没有读取 .bash_profile 的原因。