Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a JAVA_HOME variable, doing >echo $JAVA_HOME gives "old/path/to/my/jdk.1.7". I go to terminal and do a export JAVA_HOME=new/path/to/my/jdk.1.8.0 which works fine for the duration of the terminal session, but soon as I start a new tab/terminal instance and do a >echo $JAVA_HOME it gives me: old/path/to/my/jdk.1.7. What could I be doing wrong.

share|improve this question

migrated from Apr 4 '13 at 20:05

This question came from our site for professional and enthusiast programmers.

Ubuntu specific, but might help… – odysseus.section9 Apr 4 '13 at 13:12

You have to include this export in your ~/.bashrc or ~/.bash_profile so it is loaded every time you start a new session.

share|improve this answer
Thanks @fedorqui I actually had already figured it out, I posted this question to help other people that might get this problem – ThaSaleni Apr 4 '13 at 13:12
OK! Then you may want to accept the answer to make it clear. – fedorqui Aug 5 '15 at 7:51

An environment variable set in a shell session persists only for the duration of that session. To persist the variable you need to set it every time a shell session is started using a script. There are several script files that exist for this purpose and are executed automatically by bash whenever it is invoked. Unfortunately, this is not as straightforward as it seems because bash can be invoked in different ways and each type of invocation executes different scripts.

For interactive login shell invocations (console, ssh, etc.), you can set the variable by adding the following line to ~/.bash_profile, ~/.bash_login or ~/.profile. Bash will look for these files in order and execute the first one that is readable.

For interactive non-login shell invocations (subshells, terminals from graphical environments, etc.) use ~/.bashrc. Note that some distros (Ubuntu) source ~/.bashrc from ~/.bash_profile so setting it here will set it for interactive logins as well.

export JAVA_HOME=new/path/to/my/jdk.1.8.0

Also non-interactive shell invocations (shells invoked to to run a script) inherit the parent environment, but do not execute any startup scripts.

Additionally, for some distros (Ubuntu for example), the recommended file to edit is ~/.pam_environment, but the syntax is slightly different and requires one expression per line.

JAVA_HOME DEFAULT=new/path/to/my/jdk.1.8.0

If you want the changes to be system-wide rather than user-specific use /etc/bash.bashrc, /etc/profile and /etc/environment respectively.

For more information on the difference between these files see the following link.

What's the difference between .bashrc, .bash_profile, and .environment?

It might also be helpful to read the INVOCATION section of the BASH man file as there are so many circumstantial factors that influence what script gets run and when.

share|improve this answer
+1 for good explanation, quite deeper than mine. Anyway, I also have to state that providing a very long answer to a short question can make people skip it and go for others easier and faster to read and understand – fedorqui Apr 4 '13 at 14:06
Thanks and I agree. The short answer will help those that need it fixed yesterday, the long answer will help those than want to understand what is happening. – odysseus.section9 Apr 4 '13 at 15:29

You must log in to answer this question.

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