bash Mac OS X 10.9 - 设置永久环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22502759/
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
Mac OS X 10.9 - setting permanent environment variables
提问by danw
How do I set a permanent environment variable (i.e. one that does not need exporting every time I start a new Terminal session) in Mac OS X 10.9? I've found a number of answers about modifying my .bash_profile
and .profile
, however neither of these options seem to work as permanent solutions - only temporary. The variable I'm trying to set is MULE_HOME
. I have the following line in my bash profile:
如何在 Mac OS X 10.9 中设置永久环境变量(即每次启动新终端会话时不需要导出的环境变量)?我找到了许多关于修改 my.bash_profile
和的答案.profile
,但是这些选项似乎都不是永久解决方案 - 只是暂时的。我试图设置的变量是MULE_HOME
. 我的 bash 配置文件中有以下行:
export MULE_HOME=$(/opt/mule-standalone-3.4.0)
However, when I start Terminal I get the following line (not sure if this is normal behaviour?):
但是,当我启动终端时,我得到以下行(不确定这是否是正常行为?):
-bash: /opt/mule-standalone-3.4.0: is a directory
And running a simple env
command returns the following:
运行一个简单的env
命令会返回以下内容:
TERM_PROGRAM=Apple_Terminal
SHELL=/bin/bash
TERM=xterm-256color
TMPDIR=/var/folders/fc/68bqp4jj411gynj5qvwhq6z1shs1fy/T/
Apple_PubSub_Socket_Render=/tmp/launch-xKtkql/Render
TERM_PROGRAM_VERSION=326
TERM_SESSION_ID=E97BFE4B-AF85-4933-B252-0883CC085349
USER=dan
SSH_AUTH_SOCK=/tmp/launch-rEmTWW/Listeners
__CF_USER_TEXT_ENCODING=0x730C85DE:0:0
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
__CHECKFIX1436934=1
PWD=/Users/dan
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home
LANG=en_GB.UTF-8
MULE_HOME=
SHLVL=1
HOME=/Users/dan
LOGNAME=danwiseman
_=/usr/bin/env
In order to get around this I'm currently having to type export MULE_HOME=/opt/mule-standalone-3.4.0
every time I start a new Terminal session which, whilst not strenuous, is a little inconvenient. What am I doing wrong here that is causing the variable to only be set temporarily? Thanks in advance.
为了解决这个问题,我目前export MULE_HOME=/opt/mule-standalone-3.4.0
每次开始新的终端会话时都必须打字,虽然不费力,但有点不方便。我在这里做错了什么导致变量只被临时设置?提前致谢。
采纳答案by trojanfoe
Drop the $(...)
bit, which would attempt to execute the command within the brackets and set $MULE_HOME
to whatever it produces. In your case /opt/mule-standalone-3.4.0
is not an executable, hence the error you are getting.
删除该$(...)
位,它将尝试执行括号内的命令并设置$MULE_HOME
为它产生的任何内容。在您的情况下/opt/mule-standalone-3.4.0
不是可执行文件,因此您会收到错误。
export MULE_HOME=/opt/mule-standalone-3.4.0
and use ~/.bashrc
not ~/.bash_profile
.
并使用~/.bashrc
not ~/.bash_profile
。
EDIT: It seems opinion is that you should set environment variables in your ~/.bash_profile
script, and not ~/.bashrc
script.
编辑:似乎意见是您应该在~/.bash_profile
脚本中设置环境变量,而不是~/.bashrc
脚本。
回答by CodeOverRide
Just did this really easy and quick. First create a ~/.bash_profilefrom terminal:
刚刚做到这一点非常简单快捷。首先从终端创建一个~/.bash_profile:
touch ~/.bash_profile
then
然后
open -a TextEdit.app ~/.bash_profile
add
添加
export TOMCAT_HOME=/Library/Tomcat/Home
Save document in TextEdit and you are done.
将文档保存在 TextEdit 中,您就完成了。
回答by Jean-Philippe Bond
Alternatively, you can also add the following command to your .bash_profile
if you want your environment variables to be visible by graphic applications. In Mac OS X, graphic applications do not inherit your .bash_profile config :
或者,.bash_profile
如果您希望图形应用程序可以看到您的环境变量,您也可以将以下命令添加到您的命令中。在 Mac OS X 中,图形应用程序不会继承您的 .bash_profile 配置:
launchctl setenv MYPATH myvar
回答by Anthony Kong
You can put your export statement in ~/.bashrc
你可以把你的出口声明放在 ~/.bashrc
回答by BuvinJ
It seems that Apple keeps changing how to do this. And it's all about context. One way does not necessarily work when another does. I needed it to work in an IDE, and neither of the bash files mention here (Linux style) did that. The current way for GUI apps to respect this on a permanent basis is SUPER convoluted compared to Windows and Linux!
苹果似乎一直在改变如何做到这一点。这一切都与上下文有关。当另一种方法有效时,一种方法不一定有效。我需要它在 IDE 中工作,这里提到的 bash 文件(Linux 风格)都没有做到这一点。与 Windows 和 Linux 相比,GUI 应用程序目前永久尊重这一点的方式非常复杂!
In a nutshell, you have write a huge pile of ugly XML into a plist file to run some bash. That goes into your "launch agents" directory, i.e. ~/Library/LaunchAgents/my.startup.plist
. Here's another Stack Exchange thread on the subject:
简而言之,您将一大堆丑陋的 XML 写入 plist 文件以运行一些 bash。那进入您的“启动代理”目录,即 ~/Library/LaunchAgents/my.startup.plist
. 这是关于该主题的另一个 Stack Exchange 线程:
That gives you a full copy & paste which you can tweak to set your specific variable.
这为您提供了完整的复制和粘贴,您可以对其进行调整以设置您的特定变量。