1
  1. My goal is to set the environment variable JAVA_HOME for an user named grid which is just the traditional user name for hadoop.

  2. My machine is a virtual machine deployed in VMware Workstation, 64bit CentOS7.

  3. What I do: I edit the file that is supposed to change my user-specific environment variable, i.e. ~/.bash_profile. Below is my code:

    export JAVA_HOME=/home/grid/jdk
    export HADOOP_HOME=/home/grid/hadoop
    PATH=$PATH:$HADOOP_HOME:$JAVA_HOME
    export PATH
    
  4. Very interestingly, when I log in as user grid, and I run echo $HADOOP_HOME, I get /home/grid/hadoop, but I get an nothing as response when I run echo $JAVA_HOME, literally just nothing, an empty string or null, something like that. I run cd $JAVA_HOME, and I end up in home directory.

  5. I tried changing the JDK folder, it didn't work. I tried the same code on another machine, it worked. I put the code in /etc/profile and log in as root, same thing happened, good for echo $HADOOP_HOME but no response for echo $JAVA_HOME.

  • is a later part of that file unsetting JAVA_HOME? – Jeff Schaller Aug 22 '17 at 16:18
  • But that's all the code I put in .bash_profile, why would it unset JAVA_HOME? – Sam Chan Aug 22 '17 at 16:19
  • I don't know what else is in your .bash_profile, so I asked. – Jeff Schaller Aug 22 '17 at 16:21
  • I see, that's a good point. I thought about it too. Could it be some other file other than ./bash_profile that has code in it and unsets JAVA_HOME? – Sam Chan Aug 22 '17 at 16:23
  • You say you modified .bash_profile, then logged in as grid. Was the .bash_profile that you modified in the home directory of grid or of some other user? – Kusalananda Aug 22 '17 at 16:49
0

First, an item of note on $PATH. You have the JAVA_HOME set to the jdk directory, but for the PATH, you need to make that $JAVA_HOME/bin.

Second, if you need these variables to be available to non-interactive scripts, you may want to put them in ~/.bashrc, since ~/.bash_profile is read by interactive logins only.

In fact, the actual order in which (login shell) profiles are read is:

  1. /etc/profile

then:

  1. ~/.bash_profile (On CentOS, if -f ~/.bashrc, sources ~/.bashrc)
  2. ~/.bash_login
  3. ~/.profile (On Ubuntu, if -n $BASH_VERSION, sources ~/.bashrc)

EDIT: If one of these files is found, further files are not processed unless explicitly sourced

Non-login shells (by default) only look for ~/.bashrc.

So, depending on how you're executing bash, you may not even be reading the profile you've added these lines to. Are you executing sudo -u grid bash, or su grid, or su - grid, or initiating a new ssh session each time you test? Some of those options may not trigger an interactive login bash shell.

So check each of the files listed above, and see if any of them are unsetting, or resetting the JAVA_HOME variable. Then try adding the lines to the end of ~/.profile, which is the last file sourced by a login shell.

For debugging purposes you can add messages to the profile to make sure it's getting set right, too.

~/.profile:

export JAVA_HOME=/home/grid/jdk
echo "DEBUG: .profile: JAVA_HOME is [$JAVA_HOME]"
export HADOOP_HOME=/home/grid/hadoop
echo "DEBUG: .profile: HADOOP_HOME is [$HADOOP_HOME]"
PATH=$HADOOP_HOME:$JAVA_HOME/bin:$PATH
echo "DEBUG: .profile: PATH is [$PATH]"
export PATH

This will cause 3 lines of output when you login to an interactive login shell, and the values between the brackets will make it easy to see if the values are being set to begin with. If you see that JAVA_HOME is being set when ~/.profile is sourced, but is not set when the shell becomes interactive, then you know you need to keep following the login process to determine where the value is changing.

  • I put the code as above in the file ~/.profile, and run su - grid, but nothing returned, as opposed to 3 lines as you said above. That's very frustrating.. – Sam Chan Aug 22 '17 at 16:49
  • 1
    That is very strange. Are you certain the the grid user has /bin/bash as their shell? And not /bin/zsh or /bin/sh or something else? – Tim Kennedy Aug 22 '17 at 16:58
  • OOh. It seems that files are only processed in the order above until a file is found. So if you have a ~/.bash_profile, add your lines to the end of that, or consolidate all your rules into one file. – Tim Kennedy Aug 22 '17 at 17:06
  • Interestingly, it worked after I add it to ~/.bash_profile. But I don't understand why? There is no essential difference between your code and mine except that you added the debug line and bin to each of the HOME variable. – Sam Chan Aug 22 '17 at 17:10
  • Well, it seems if you have a ~/.bash_profile, it gets processed and the other files are not read. Subsequent files (.bash_login, .profile) are only read if the file before them in the list doesn't exist. – Tim Kennedy Aug 22 '17 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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