How To Set Environment Variables in Linux


Environment variables define the behavior of processes running on the system. These variables store information about the computing environment, and applications can access it to adapt their behavior accordingly. Hence, environment variable configurations impact your Linux system and its applications. Moreover, you can customize the computing environment by setting environment variables. 

Therefore, understanding how to declare environment variables is essential for all Linux users. It undoubtedly enhances efficiency and productivity. This short blog will discuss different ways of setting environment variables without difficulties.

The export Command

You can use the export command to set an environment variable. To use it, enter the below command in the terminal:

This command would set an environment variable MY_VARIABLE with its value as “value.” You can use the command similarly by replacing “MY_VARIABLE” and “value” with your desired variable name and value, respectively. For example, to set a variable named PRATEEK_EV with the value “Hello World!”, enter:

export PRATEEK_EV=“Hello World!”


On successful execution, it does not display anything, but you can confirm it using the printenv command.


The set Command

The set command is an alternative to the export command, which creates variables temporarily within a script or session:

Again, replace the values according to your preferences. For instance, retaking the above example:

set PRATEEK_EV=“Hello World!”


Permanent Environment Variables

The environment variable you create using the set and export commands is temporary and stays in the system until you close your shell session. If you plan to set an environment variable for more extended periods, export it in the bash configuration file.

The tildes(~) symbol indicates the home directory, and bashrc is the name of bash’s configuration file. For zsh or fish shells, you can use “nano ~/.zshrc” and “nano ~/.config/fish/” respectively. Now, append the below command into the file and save it:

export MY_ENV=“Information”


A Quick Wrap-up

Environment variables help shape the behavior of applications and scripts on Linux systems. Despite this importance, users often need to understand how to set environment variables. Hence, this blog provides the three easiest ways of setting environment variables using suitable examples. 

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More