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.
How to Set Environment Variables in Linux
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 you’d like to know more about shell and other related commands I recommend the following books, both of which are great for beginners:
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.
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 in Linux
You can check all the set variables using the printenv or the env command:
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 Environment Variables in Linux
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:
And then try to print using this:
You can see that the local variable, LOCAL_VAR, is not accessible while the global variable, GLOBAL_VAR is.
How to Set Environment Variables in Linux
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:
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/.
How to Unset Global Environment Variables in Linux
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
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.
And that’s it! Good luck with your variables!