Mac OS X 10.9 - setting permanent environment variables

a 夏天 提交于 2019-12-17 15:32:45

问题


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:

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:

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.


回答1:


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.

export MULE_HOME=/opt/mule-standalone-3.4.0

and use ~/.bashrc not ~/.bash_profile.

EDIT: It seems opinion is that you should set environment variables in your ~/.bash_profile script, and not ~/.bashrc script.




回答2:


Just did this really easy and quick. First create a ~/.bash_profile from terminal:

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.




回答3:


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 :

launchctl setenv MYPATH myvar



回答4:


You can put your export statement in ~/.bashrc




回答5:


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!

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:

https://apple.stackexchange.com/questions/106355/setting-the-system-wide-path-environment-variable-in-mavericks

That gives you a full copy & paste which you can tweak to set your specific variable.



来源:https://stackoverflow.com/questions/22502759/mac-os-x-10-9-setting-permanent-environment-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!