July 4, 2024
Tutorials

How to Set Environment Variables in Linux

How to Set Environment Variables in Linux

Environment variables comprise names and values that define the system environment. These variables store essential information, including paths, system names, and other configuration settings. With environment variables, you can automate tasks and control the behavior of programs within the operating system environment.

Through this guide, you can set environment variables temporarily, permanently, and globally in your Linux environment.

How to Set Environment Variables in Linux?

This article explores the following parts:

  • Set Environment Variable Temporarily
  • Configuring Environment Variable Permanently
  • Setting up Environment Variable Globally 

How to Set Environment Variables Temporarily in Linux

These steps contribute to set environment variables temporarily in the Linux system. 

Step 1: View Current Environment Variables

To view all the current environment variables present in your Linux system, use the command:

printenv 

You will see a list of variables along with their values on your Linux system after executing the command. 

Step 2: Set Temporary Environment Variable

You can set an environment variable only for the current session. It will be removed after rebooting your system. Here is the command:

export TEST_ENV="Test Value"

Step 3: Verify Environment Variable

To examine the complete list of your system’s environment variables, utilize the “printenv” command. However, here we will utilize the “grep” command to verify only the newly created environment variable:

printenv | grep TEST_ENV

After the successful execution of the command, the variable along with its value will be displayed on your terminal.

How to Configure Environment Variables Permanently in Linux?

Follow these steps for setting up the environment variable permanently in the Linux system. 

Step 1: Access and Edit “.bashrc” File

The Linux operating system allows you to establish an environment variable not only for a short time but permanently. To do so, you need to make some changes in the “.bashrc” file:

sudo nano .bashrc

In the ".bashrc", type command at the end and close after saving the file:

export TEST_ENV="Set the test value as permanent"

Step 2: Reload “.bashrc” File

After editing the “.bashrc” file, you need to reload to apply the changes to the current session without rebooting your system:

source .bashrc

Step 3: Confirm Environment Variable

Once the ".bashrc" file is reloaded, you can confirm whether the environment variable is set permanently by using the command:

printenv | grep TEST_ENV

You can also use the “env” command to view the complete list of environment variables on your Linux system:

env 

After setting the environment variable permanently, you can find it (TEST_ENV=Set the test value as permanent) in your variables list. 

How to Set up Environment Variables Globally in Linux?

With these steps, you can make environment variables globally that can be accessed by all users on the system.

Step 1: Create Backup of the “/etc/profile”

Before start editing the /etc/profile, create a backup using the provided command:

sudo cp /etc/profile /etc/profile_backup

This process will secure your profile file if the file corrupts. Later, the file can be restored from the backup file. 

Step 2: Access and Edit “/etc/profile” File

Next, open the “profile" through command:

sudo nano /etc/profile

In the profile, scroll down to the end and type the command and then save it:

export TEST_ENV="Set the test value as Global"

Step 3: Reload the “/etc/profile”

From your terminal window, run the source command followed by /etc/profile to apply changes without rebooting you machine:

source /etc/profile

Step 4: Verify Environment Variable

Upon executing the source command, run the following command to view the environment variable is set globally:

printenv | grep TEST_ENV

Step 5: Confirm Environment Variable on Another User

You can confirm whether you can access the environment variable from another user. To do so, login to another virtual environment and use the same command:

If you see the variable name (TEST_ENV) and its value (Test Value as Global), it shows that you have successfully set the environment variable globally. 

Conclusion

In Linux, you can configure an environment variable temporarily, permanently, and globally. To set up a variable utilize the export command followed by variable_name=value. However, you can make it permanent by placing it in the “.bashrc", while making it available globally, edit the /etc/profile with export variable_name=value

In this article we have explored setting up an environment variable on Linux-based systems.

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video