Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 stackoverflow.com Apr 4 '13 at 20:05

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

    
Ubuntu specific, but might help stackoverflow.com/questions/2655641/… –  odysseus.section9 Apr 4 '13 at 13:12
add comment

2 Answers

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
1  
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
add comment

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.

http://linux.die.net/man/1/bash

share|improve this answer
1  
+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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.