How to Set Environment Variables in Linux

Setting environment variables in Linux is a process with a wide range of applications for data scientists, machine learning engineers and programmers. This guide will help you get started with the process.
Headshot of author Rahul Agarwal
Rahul Agarwal
Expert Columnist
May 17, 2022
Headshot of author Rahul Agarwal
Rahul Agarwal
Expert Columnist
May 17, 2022

Have you seen coders work day in and out on those black (sometimes green) screens and wondered what sort of wizardry is happening? Those green screens are called terminal/shell/bash and have now become a constant part of the life of every machine learning engineer, data scientist and programmer. We use Linux shells for a variety of tasks: logging into servers, writing complex scripts, doing various automations and running various programs. 

Sometimes, we might need to store information about default text editors or paths to wherever Python or Java is stored so that every process on your system can access them. In such cases, you might have seen the use of commands like export and unset in the Linux shell. These commands are used to set and unset something called an environment variable. In this article, I will explain environment variables, why and when you want to use them and how to set them up.

For background, if youd like to know more about shell and other related commands I recommend the following books, both of which are great for beginners:

  1. The Linux Command Line
  2. Learning the bash Shell: Unix Shell Programming

The first book is more of a fun read while the second one is a little more serious and goes into more depth. The UNIX workbench course on Coursera is another great resource here as well.

How to Set Environment Variables in Linux

Setting up environment variables is pretty easy. The process depends upon the purpose you have for setting them up. So, for example, if you want to set them up globally, which is the most common use case, you would use the export command: export GLOBAL_VAR="/usr/raha/" If you would like to set this variable whenever you open up your shell, you can put this export statement in the bash_profile.sh file. This ensures that the next time you open up a new shell instance, your GLOBAL_VAR will be set to /usr/raha/.

More From Rahul AgarwalConfidence Intervals, Explained!

 

What Is an Environment Variable? 

An environment variable is any variable that can be accessed by any process on a computer. Simply stated, they are just variables that can be used in a shell or the command line and its subprocesses. So, for example, you might have installed Anaconda and might have needed to set a global environment variable called “PATH” in your bash_profile. Even if you didn’t set it, the variable might have been added automatically for you. If you want, you can check it:

  • Open the terminal app in Linux
  • Run cat ~/.bash_profile
  • You should be able to see something like this: export PATH=/Users/raha/anaconda3/bin:$PATH”

This command usually appends a new binary path to existing path variable so that the shell knows the location of the Anaconda binary. It exists in ~/.bash_profile so that it’s initialized automatically for every user. This ensures that the next time you open a Jupyter Notebook, the process knows where to look for the Anaconda binary. 

 

Checking Environment Variables

You can check all the set variables using the printenv or the env command:

A Linux output
Image: Screenshot by the author.

You can also look at a single environment variable using the echo command:

echo $PWD
Output: /Users/raha
echo $SHELL
Output: /bin/zsh

 

Local And Global Variables

In Linux, environment variables can also be categorized into local and global variables, just as in many common programming languages. In Python, for example, a global variable is one that can be used anywhere in the program, whereas a local variable is defined in a function and can only be used in that particular function. 

In Linux, an environment variable can also be local, which means it’s only accessible inside the shell in which it’s initialized. By contrast, you can access a global variable in all the sub shells. 

This is why, when you open a Jupyter Notebook in a shell, it can access the shell variables in the main shell it’s started from using the ! commands. So, if you do something like this: 

A Linux terminal
Image: Screenshot by the author.

And then try to print using this:

A Linux printout
Image: Screenshot by the author.

You can see that the local variable, LOCAL_VAR, is not accessible while the global variable, GLOBAL_VAR is. 

 

How to Set Environment Variables 

Setting up environment variables is pretty easy. The process just depends upon the purpose you want to solve by setting them up. So, for example, if you want to set them up globally, which is the most common use case, you would use the export command:

export GLOBAL_VAR="/usr/raha/"

If you would like to set this variable whenever you open up your shell, you can put this export statement in the bash_profile.sh file. This ensures that the next time you open up a new shell instance, your GLOBAL_VAR will be set to /usr/raha/.

More in Software Engineering5 Ways to Learn Git and Version Control

 

How to Unset Global Environment Variables

Unsetting a set environment variable is pretty straightforward as well. You can just use the keyword unset. So, if you were to unset an environment variable, you can use:

unset LOCAL_VAR

unset GLOBAL_VAR
A Linux terminal
Image: Screenshot by the author.

You can also use `unset LOCAL_VAR GLOBAL_VAR` to unset them both. 

What happens if you unset a global variable in a subshell? Just what you would expect. It gets unset in the subshell while it persists in the main shell.

A Linux terminal
Image: Screenshot by the author.

And that’s it! Good luck with your variables!

Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Jobs at Meta

Great Companies Need Great People. That's Where We Come In.

Recruit With Us