Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

This question already has an answer here:

I'm trying to set the JAVA_HOME environment variable in my .bashrc but every time I log back in it gets unset again to its original value, below is my .bashrc file

# .bashrc

# User specific aliases and functions

export JAVA_HOME=/usr/java/latest

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

What am I doing wrong? Thanks

share|improve this question

migrated from stackoverflow.com May 3 '12 at 12:48

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

marked as duplicate by Gilles, Renan, jasonwryan, vonbrand, slm May 31 '13 at 0:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
"unset again to its original value" Which is it? – Ignacio Vazquez-Abrams May 1 '12 at 17:11
    
As answered below, you should make sure you source other configuration files before your own statements. It may have been overwritten by the global /etc/bashrc file. – user13742 May 3 '12 at 13:41
    
@Gilles I vote against closing because the solution to his problem (order of file execution) is not even mentioned in the other thread. The subject is similar but the perspective quite different. – Hauke Laging May 30 '13 at 23:28
    
@HaukeLaging This isn't about the order of file execution, it's about using the wrong file (.bashrc instead of .profile). – Gilles May 30 '13 at 23:29
up vote 1 down vote accepted

Is JAVA_HOME being set in /etc/bashrc? If so, then you should put your custom value after the block where this file is sourced.

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User environment vars should source *after* global environment vars 
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

export JAVA_HOME=/usr/java/latest
share|improve this answer

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