I have always difficulties setting the Environment variables in OS X like JAVA_HOME, M2_HOME or PATH. How to set these on OS X Mavericks?

I did some research but the answers I've found didn't really help me yet:

  1. export JAVA_HOME=/... (But it seems that the changes are temporary, just for one terminal session.)
  2. setenv JAVA_HOME=/... (But command not found.)
  3. Open .profile and write the variables inside to make the changes permanent- (.profile does not exist).
  4. Open .bash_profile and write the variables inside to make the changes permanent- (.bash_profile does not exist).
  5. vi ~/.bash_profile (Quite a challenge for somebody who doesn't know vi.)
  6. Creating your own enrivonment.plist file.

Can somebody please walk me through the steps to get that to work on OS X Mavericks, assuming no Unix knowhow?


I have a .profile in my home directory; it contains many export … statements for environment variables.

You can create such a file by opening a Terminal and issuing the command touch .profile Close Terminal.

Then you should open that file in a plain-text editor (TextWrangler for example). You can also use nano .profile in a Terminal window (current directory should be your home), which is much easier than vi. Insert lines such as export JAVA_HOME=… . Save, exit nano if you used that and quit a running Terminal.

Open Terminal and issue the command env to see all environment variables. Check that the ones you defined have the value you assigned to them. You should be good to go now. But don't forget that environment variables defined in .profile are not passed to GUI applications.

  • Thank you very much. This was what i really looking for. I now have set all my variables. What do u mean by GUI applications? I just needed some variables for java and maven in order to work with eclipse, spring tools etc. Eclipse is an application with GUI (Graphical User Interface). Do you mean this by GUI? – akcasoy Oct 25 '13 at 18:32
  • Ok. I have read some and i think you have mean really GUI with GUI.. Is there a way to make these variables available everywhere? What is the most common way or where is the most common location to define them then? – akcasoy Oct 25 '13 at 18:49
  • 1. yes that is what I meant by a GUI app. 2. setting environment variables for GUI app's in OS X 10.8 appears to be quit difficult. Some apps let you define environment variables, which are to be passed to other applications, in their preferences for example. You can also use the open -a Appname method in a Terminal session. – Bhas Oct 26 '13 at 9:37
  • 5
    I don't know why but for me (OS X Yosemite 10.10.1) the .profile didn't help. I had to put the export statements into the .bash_profile to make it work. Hopefully this helps someone else if he runs into the same problems... – chuky Dec 4 '14 at 19:50

In Yosemite, you should put export VARIABLE='something' inside .bash_profile.

I've tried writing the export VARIABLE='something' inside .profile without success.


From http://hathaway.cc/post/69201163472/how-to-edit-your-path-environment-variables-on-mac:

  • Open Terminal
  • Run touch ~/.bash_profile; open ~/.bash_profile
  • In TextEdit, add

    export PATH="$HOME/.rbenv/bin:$PATH"
  • Save the .bash_profile file and Quit (Command + Q) Text Edit.

  • Run source ~/.bash_profile

In case you're using zsh like me, you need to modify ~/.zshrc.

  • 1
    Or in .zprofile – timetofly May 16 '19 at 12:58
  • Some of my application add something to PATH, but I didn't see the ~/.zshrc or .zprofile you said? @timetofly – Raining Apr 16 '20 at 6:57
  • You can try creating that file in your home directory and see if zsh picks it up. – timetofly Apr 17 '20 at 17:27
  • 1
    Starting with macOS Catalina, Macs use zsh as the default login shell and interactive shell - support.apple.com/en-us/HT208050 – Vadim Kotov Nov 20 '20 at 11:18
  • yes!! thank you sir! – ennth Feb 13 at 21:28

For adding a directory to a path, there is a better option in OS X: All entries in the file /etc/paths are added to the path, before any shell is started.

Edit this file with:

sudo pico /etc/paths

For more info, see: https://gist.github.com/Overbryd/1669348

  • Thank you! for me this is the most elegant way to include a new path – Asimov Oct 31 '16 at 21:54

For those who don't like to have the hassle with text files and editors, there is a GUI tool as well on GitHub.

  • Great ! I spent whole day figuring out to solve the environment variable issue and I was nowhere .The app that you posted solved all my environment hassles – Shajo Apr 22 '15 at 8:08

OSX has had only one change re environment variables and that was in Lion where ~/.MacOSX/environment.plist was removed. Although there also has been a change in the default shell in 10.2 or 10.3 from tsch to bash.

You need to define what you want the enviroment variable set for and what environment you have.

You also need to understand shells and Unix which by your comments you have not fully done. There are several nbash tutorials around.

As to your points

  1. You understand correctly assuming you are in a sh type shell (e.g. bash or zsh)
  2. This is for C shells e.g. tcsh so won't work on recent defaults.
  3. You need to create .profile then it works
  4. It is .bash_profile or better .bashrc and you need to create the file first
  5. Totally correct :) Use TextEdit or nano (or emacs)
  6. Correct up to OSX 10.7 and the wy to do this for programs called from the Workspace e.g. from Dock or Finder (or open)

So 3 or 4 work if you are calling the program from the command line (or from a program started in the command line but not by open)

As for programs from the GUI see this question

As for internet is full of rubbish - you need to have enough background to understand an article because as you have discovered many assume things or are incorrect. StackExhnage sites should be better as you can see if an answer has been agreed to from the number of votes.

  • I still do not have any result. The linked question does not bring me to solution. I have a mac for 4 years, yet still do not have (didn't have to) experience about UNIX. I think, when the answer of "Why is it damn so hard?" is "plist was removed, change in 10.2 or 10.3, sh type shell, up to 10.7 etc.." the answer contradicts with itself. – akcasoy Oct 25 '13 at 13:37
  • It could be made very easily like in windows. Anyway.. what i was really expecting was sth. like this: 1- open home dir 2- open terminal 3- write "xxx" 4-click save etc.. one must not have enough background to set just a simple variable, at least could find a proper solution from those boilerplate answers on the internet. – akcasoy Oct 25 '13 at 13:41
  • If you need environment variables you have to understand what they are doing and that requires background. Also as you did not state what you were using the variables for I have to give all the possible differences. Note for Java setting JAVA_HOME is not necessarily the best way (see Java questions) – mmmmmm Oct 27 '13 at 17:25
  • My question was about SETTING the variables. Neither about what they are doing, nor about any other thing which requires more info. You can obviously set them even without background. Thank you for your answer though. But as i already wrote, a simple proposal for solution was enough, which i already have thanks to Bhas. – akcasoy Oct 30 '13 at 22:37
  • You really should not set variables without background you have to know what they do – mmmmmm Oct 30 '13 at 22:55

Create file on Home directory : .bash_profile (if not have)

Open file with text editor : vim / nano (This is a hidden file, may not visible on Finder)

Add env variables (add these lines) : export VARIABLE_NAME=VARIABLE_VALUE (Don't add spaces between variable name and value)

Save and Exit.

Run : source .bash_profile at home directory on terminal.

Check : Open new terminal and run echo $VARIABLE_NAME. (should display the VARIABLE_VALUE)

PS : These variables may not load for some IDEs, need to load with .env file.

  • Welcome to Ask Different and taking the time to answer a question. However, when writing an answer you should ask yourself How is this different from any of the already posted answers? To help you acclimate, please see How to write a good answer in our Help Center – Allan Nov 20 '19 at 3:56

Open terminal


input : echo $SHELL output : /bin/zsh

if your machine uses zsh then use below commands|

Command - 2

vi ~/.zshrc

it will open new file or edits existing file.

now add below line and press esc and enter :wq to save and close the file. export JAVA_HOME=$(/usr/libexec/java_home)

Command 3:: to apply the changes

source ~/.zshr

Command 4::

echo $JAVA_HOME /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

you will see output as per above line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .