Linux shell has become a constant part of every ML Engineer, Data Scientist and Programmer’s life. We need linux to login to servers, write complex scripts, and run various programs. And sometimes we need linux to install certain programs too.
In such particular cases, you might have seen usage of commands like export
and unset.
In this article, I will try to explain environment variables, why and when they are used, and how to set them up.
As per Wikipedia, “An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. The variables can be used both in scripts and on the command line .”.
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 used Anaconda and might have needed to set an global environment variable called “PATH” in your bash_profile
. Or even if you didn’t set it, it might have been added automatically for you. If you want you can check it:
cat ~/.bash_profile
export PATH=”/Users/raha/anaconda3/bin:$PATH”
What this command usually does is:
~/.bash_profile
? So that it is initialized automatically for every user.This makes sure that the next time you run jupyter notebook
command in your terminal to open a jupyter notebook, the process knows where to look for the anaconda binary.
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
In Linux, Environment variables can also be categorized into local and global variables just like many common programming languages. So for example, in Python, a global variable is one that can be used anywhere in the program while 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 is only inside a shell in which it is initialized while a global variable can be accesed in all the sub shells.
This is why, when you open a Jupyter Notebook in a Shell, it is able to access the shell variables in the main shell it is started from, using the ! commands. So if you do something like:
And then try to print using:
You can see that the local variable, LOCAL_VAR is not accesible while the global variable GLOBAL_VAR is accesible in the Jupyter Notebook.
Setting up environment variables is pretty easy. It just depends upon the purpose you want to solve by setting them up. So, for example if you want to set them up globally (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 it the above export statement in the bash_profile.sh
file. This makes sure the next time you open up a new shell instance your GLOBAL_VAR
would be set to “/usr/raha/”
Unsetting a set environment variable is pretty straightforward. 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
or 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. For Example:
If you would like to know more about shell, which I guess you would, here are some books that I would recommend for a beginner:
The first book is more of a fun read while the second one is a little more serious and goes much more deeper. Also If you would like to learn more using a course, there is The UNIX workbench course on Coursera which you can try out.
comments powered by Disqus