Nov 10, 2018

How to Set Environment Variables in Linux

You can put the export statement such as
export JAVA_HOME=/usr/java/jdk1.8.0_11
into

/etc/profile File for Linux system wide environment

~/.bash_profile ($HOME/.bash_profile) or ~/.prfile file For only this account's environment.

The above only runs for login shell. If you wanted to make large changes or application specific changes, you can try putting a script to gather the variables in /etc/profile.d/

Example:

/etc/profile.d/java.sh

$ cat java.sh
export JAVA_HOME=/usr/java/jdk1.8.0_11
/etc/profile does a call that will run any script in /etc/profile.d/, and this applies to all users on the system including root.

If you want to load the environment variables within java.sh without having to restart the machine, you can use the source command as in:
$> source java.sh
Check to make sure it works
echo $JAVA_HOME
And you can use
$ set
to display current environment.