How to Set Environment Variables in Linux?
Here you will understand how to set environment variables in Linux. We have covered topics such as setting, unsetting, listing environment variable etc. Let’s explore.
In Linux, environment variables are a set of dynamic named values that can affect the behavior of various processes running on the system. These variables are used to store information about the system environment and can be accessed by any method running on the system.
Environment variables contain information about the current user, the system, and the shell session. They can be used to set particular program preferences or define system-wide settings.
Must read: What is Linux?
For example, the “PATH” environment variable defines the directories where the system should search for executable files when a user enters a command.
Some of the common environment variables in Linux are:
- HOME: The home directory of the current user.
- PATH: The list of directories that the shell uses to look for executables.
- USER: The name of the current user.
- SHELL: The path to the current user’s shell.
- TERM: The terminal type being used.
You can also explore: Linux courses
Setting Up Environment Variable in Linux
We generally make use of the following syntax to set up an environment variable in Linux:
export <variable_name> = <variable_value>
Follow the below steps to set up the environment variable:
Step 1: Open a terminal by either pressing “Ctrl+Alt+T” on your keyboard or searching for “Terminal” in your application menu.
Step 2: Now determine the name and value of the environment variable you want to set. For example, let’s say you want to set an environment variable named “MY_VAR” with a value of “hello“.
Step 3: Now set the environment variable using the “export” command in the terminal.
export MY_VAR=hello
This sets the value of the “MY_VAR” environment variable to “hello“. Note that the variable name and value are separated by an equal sign, with no spaces. Take a look at the below image for reference:
Must read: Difference between Linux and Unix
Step 4: Now we need to verify that the environment variable has been set. For this, we can use the “echo” command to display the value of the variable:
echo $MY_VAR
This should display “hello” in the terminal as shown below:
Step 5 (Optional): If you want to make the environment variable persistent so that it’s available in future terminal sessions, you can add the “export” command to your shell’s startup script.
For example, if you’re using the Bash shell, you can add the following line to the end of your ~/.bashrc file:
export MY_VAR=hello
This will set the “MY_VAR” environment variable to “hello” every time you start a new Bash session.
That’s it! You have now successfully set an environment variable in Linux.
Also read: How to list files in Linux?
Best-suited Linux courses for you
Learn Linux with these high-rated online courses
UnSetting Environment Variable in Linux
We generally make use of the following syntax to unset an environment variable in Linux:
unset <variable_name>
Follow the below steps to unset the environment variable:
Step 1: Open a terminal by either pressing “Ctrl+Alt+T” on your keyboard or searching for “Terminal” in your application menu.
Step 2: Now determine the name of the environment variable you want to unset. For example, let’s say you want to unset an environment variable named “MY_VAR” with a value of “hello“, that we created earlier.
Step 3: Now unset the environment variable using the “unset” command in the terminal.
unset MY_VAR
This will remove the “MY_VAR” environment variable from the current shell session. Take a look at the below image for reference:
Step 4: To verify that the environment variable has been unset correctly, you can use the “echo” command to display the value of the variable:
echo $MY_VAR
If the environment variable has been unset correctly, this should display an empty line in the terminal as shown below:
Step 5 (optional): If you want to make the environment variable unset persistent so that it’s not available in future terminal sessions, you can remove the “export” command from your shell’s startup script.
For example, if you added the following line to your ~/.bashrc file to set the “MY_VAR” environment variable:
export MY_VAR=hello
You can remove that line from your ~/.bashrc file to make the variable unset permanently.
Listing All Set Environment Variables
To list all set environment variables in Linux, you can use the printenv command.
Syntax:
printenv
Take a look at the below image for reference:
This will display a list of all set environment variables and their values in the terminal.
If you want to search for a specific environment variable, you can use the grep command to filter the output.
For example, to search for the “PATH” environment variable, you can use the following command:
printenv | grep PATH
This will display only the “PATH” environment variable and its value. Take a look at the below image for reference:
At this point, you have now successfully listed all set environment variables in Linux.
Permanent Global Environment Variables for All Users
Follow the below steps to set up a permanent global environment variable on a Linux system:
Step 1: Firstly we need to open a terminal with root privileges.
Step 2: Now we need to determine the name and value of the environment variable we want to set. For example, let’s say you want to set a global environment variable named “MY_GLOBAL_VAR” with a value of “hello“.
Step 3: The system-wide environment file is located at /etc/environment. Use your preferred text editor to edit the file. For example, to edit the file using the Nano editor, use the following command:
sudo nano /etc/environment
This will open up the nano editor as shown below:
Step 4:Now add the environment variable to the file by adding a line to the file in the following format:
VARIABLE_NAME="value"
For example, to set the “MY_GLOBAL_VAR” environment variable to “hello“, add the following line to the file:
MY_GLOBAL_VAR="hello"
Take a look at the below image for reference:
Step 5: Now to save and close the file in Nano, press “Ctrl+X“, then press “Y” to confirm that you want to save the changes, and finally press “Enter” to confirm the file name.
Step 6: Now run the following command to reload the system-wide environment file and make the environment variable available to all users:
source /etc/environment
source /etc/environment
Now, you have successfully set up a permanent global environment variable for all users in Linux.
Export Environment Variable
You can use the export command to export an environment variable in Linux. This allows allowing new child processes to inherit the variable.
Follow the below steps to export an environment variable in Linux:
Step 1: Open a terminal by either pressing “Ctrl+Alt+T” on your keyboard or searching for “Terminal” in your application menu.
Step 2: Use the following command:
export VARIABLE_NAME=value
Replace VARIABLE_NAME with the name of the environment variable you want to export, and replace value with the value you want to set for the variable.
For example, to export an environment variable named “MY_VAR” with a value of “hello“, type the following command:
export MY_VAR=hello
Step 3: To verify that the environment variable has been exported, you can use the printenv command to list all set environment variables.
For example, to verify that the “MY_VAR” environment variable has been exported, type the following command:
printenv | grep MY_VAR
This will display the value of the “MY_VAR” environment variable in the terminal as shown below:
Conclusion
In this tutorial, you learned how to work with environment variables in Linux, regardless of which distribution you use, from Debian to Red Hat. You discovered how to set and unset environment variables for a single user and set them up system-wide for all users.
Contributed By: Raju Kumar
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio