Linux Commands

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:

export MY_VARIABLE=value

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!"

export-command-in-linux

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

printev-command-in-linux

The set Command

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

set MY_VARIABLE=value

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

set PRATEEK_EV="Hello World!"

set-command-in-linux

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.

nano ~/.bashrc

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/config.fish” respectively. Now, append the below command into the file and save it:

export MY_ENV="Information"

export-command-results-in-linux

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. 

About the author

Prateek Jangid