What is Environment Variable in Linux? The variable is a path of the executable files that Linux systems can recognize globally on the Command terminal or by any other application. For example, We can open firefox via the command terminal by simply typing its name rather than the whole path of the actual executable file where it resides. In the same way when we install Java its executable binary can be used by other applications easily even GUI ones.
This is all possible because the Linux operating system knows where the program is located i.e Environment Variable path. This is exactly where it looks. This is determined using the path variables.
In more simple words Variables are only folder paths in which the operating system searches for executable programs. Of course, you may have to adapt or add a path here. That would be the case, for example, if you installed the popular programming language Python and needed the path for the executable Python programs. Usually, however, the path is created automatically when Python is installed on Linux. However, there are many scenarios where the packages we install, their path will not get added automatically. Hence, to do that manually, here are the commands to follow.
Add path variables or environment variables in Linux- Permanently
We can add a path temporarily but that will go off once we close the terminal or restart our system. Hence, it is better to add the required application folder path permanently.
Open command terminal
If you are using any Server Linux distribution with CLI then you are already there whereas the GUI Dekstop users can open the Terminal application to access CLI.
Find the Path of your executable
Now, note down the path which you want to add to your system variable. For example, lets’ say you want to run Apache Jmeter script to access its GUI application but without switching to its folder again and again.
For instance, We have Jmeter in “
/opt/jmeter/bin“, so this will be the folder path that we want to add in our system variables. Of course, it will be different in your case.
Add the Environment variable permanently
Now, use the given command to add the folder path in your Bashrc file to declare it as a permanent system variable.
echo 'export PATH="$PATH:/path-to-executable-folder"' >> ~/.bashrc
path-to-executable-folder in the above command with yours. For example, here we want to add Jmeter folder path, then the command will be:
echo 'export PATH="$PATH:/opt/jmeter/bin"' >> ~/.bashrc
Source Bash File
To let the Command terminal know that we have added a new folder path as system variables. Reload the file using the below-given command:
In this way, we can very easily add any folder path to our environment variable on any Linux system.
To check all system variables