Environment Variables are used to store important file locations and values that are used by applications and services in Linux. Sometimes you may need to add or modify environment variables in Linux. In this article, we will look at how to set environment variable in Ubuntu. We will also look at how to set environment variables globally and permanently. You can use these steps to set environment variable in almost every Linux distribution.
How to Set Environment Variable in Ubuntu
There are multiple ways to set environment variables in Ubuntu. We will look at each of them one by one.
Set environment variable for current session
If you want to set environment variable only for current session, simply use the variable name and assign it a value. You can do this directly from terminal.
VARIABLE="value"
Here is an example to set MY_HOME environment variable to /home/ubuntu.
# MY_HOME="/home/ubuntu"
Also read : How to Install Webmin in Ubuntu
You can display the environment variable using echo command. Add a $ sign at the beginning of your variable while using echo command.
# echo $MY_HOME # /home/ubuntu
If you want to set environment variable for not only current session but all processes starting in the current session, then use the EXPORT command as shown below
EXPORT MY_HOME="/home/ubuntu"
However, please note, these environment variables are valid for current session. They will not be available once log out of your session and log in again.
Also read : How to Configure Iptables in Ubuntu
Permanently Set Environment Variable
If you want to permanently set environment variable that is applicable for all future sessions then you need to place it in .bashrc file in your $HOME directory
Open .bashrc file
# sudo vi ~/.bashrc
Add the following line to set environment variable MY_HOME
EXPORT MY_HOME="/home/ubuntu"
Save and close the file.
Apply changes using the source command as shown below
# source ~/.bashrc
Test it using echo command
# echo $MY_HOME # /home/ubuntu
Also read : How to Use NGINX Reverse Proxy with NodeJS
Set environment variable globally
If you want to set environment variable globally for all users and processes, then add the environment variable in /etc/environment file
Open the /etc/environment file
# sudo vi /etc/environment
Add the following line to set environment variable MY_HOME. Do not use EXPORT in the following line.
MY_HOME="/home/ubuntu"
Save and close the file.
Also read : How to Convert Image to PDF in Ubuntu
Apply changes using source command.
# source /etc/environment
Test it using echo command.
# echo $MY_HOME
# /home/ubuntu
That’s it. It is important to set environment variable at the right location to have the appropriate effect. To summarize, if you want to set it in current session, then you can do so from the terminal. If you want it to be permanent for a given user, then set it in .bashrc file for that user. If you want to set the variable globally for all users, on a permanent basis, then add it to /etc/environment file.
Also read : How to Search in VI Editor
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.