An environment variable like JAVA_HOME can be set in different ways in linux systems.
#1 . If we set environment variables in .bashrc file found in each users home directory, it will be accessible only for that user.
~/.bashrc ——> specific to a particular user.
Setting environment variable for all users.
If the environment variable to be available for all users,
#2. it can be set in /etc/environment
For instance let see how JAVA_HOME can be set,
Open this file in vim or any editor of your choice, and add following line in it.
This sets JAVA_HOME for default open JDK. Please note that location is specified in quotes,
Then save the file and run the command, to have these these changes in effect immediately.
But this is not a recommended method.
#3. Setting via /etc/profile
vim /etc/profile (open the this file in vim )
Then save the file and run following command.
#4. Setting via /etc/profile.d
This can be viewed as an extension to the above step. Having all application specific environment file in /etc/profile makes the maintenance a hassle. As a solution to this, variables will be specified as a separate module inside the directory /etc/profile.d/. These modules are shell scripts and should end with .sh extension, otherwise system is not going to recognize it. You can read further on this from here.
I think this would be clear, with the following example.
In order to set JAVA_HOME bythis approach, first create a java_dev.sh file inside /etc/profile.d (The file should end with .sh extension ).
Then open this file using vim or any editor
Then add following lines ( This JAVA_HOME location could be different for others)
Then save the file and run the following command.
You can also consider reading this