You’ve probably seen coders at work on those black (or sometimes green) screens, and wondered what sort if wizardry is taking place. The green screens are known as terminal/shell/bash. They have become an integral part of the lives of every data scientist, machine learner, and programmer. Linux shells are used for many tasks, including logging in to servers, writing complex scripts, and running programs.
Sometimes, we might have to So that every process on your computer can access the information, store default text editors and paths to where Python or Java are stored. You might have seen commands such as export and unset in Linux shell. These commands are used for setting and unsetting an environment variable. This article will explain what environment variables are, how to set them up, and why you should use them.
For background,If you’re interested in shell and related commands, I recommend these books:
The first book is more fun and more informative than the second. The UNIX workbenchCoursera is another great resource.
Linux Environment Variables: How to Set Them
It is very easy to set up environment variables. It depends on what purpose you want to set them up. For example, if you wish to set them up globally (which is the most common use), you would use the export command.
export GLOBAL_VAR=”/usr/raha/”
This export statement can be placed in the bash_profile.sh script to set the variable every time you open up a shell instance. This will ensure that your global_var is set to “usr/raha” the next time you start a new shell instance.
More from Rahul AgarwalConfidence Intervals, Explained
What is an Environment Variable?
An Environment variableIs there anything?Variables that can be accessed by all processes on a computer. They can be used in a shell, the command line and its subprocesses. For example, Anaconda might have been installed and you may have needed to set the global environment variable PATH in your bash_profile. Even if it wasn’t set, it might have been added automatically. You can inspect it if you wish:
- Open the terminal app in Linux
- Run cat ~/.bash_profile
- This should show you what it looks like: export PATH=/Users/raha/anaconda3/bin:$PATH
This command adds a new binary path (or path variable) to an existing path variable, so that the shell knows where the Anaconda binary is located. It is located in /.bash_profile and is initialized automatically for each user. This ensures that every time you open a Jupyter Notebook the process knows where the Anaconda binary is.
Checking Environment Variables
You can check all set variables with the printenv and env commands:
You can also use echo to look at one environment variable:
echo $PWD
Output: /Users/raha
echo $SHELL
Output: /bin/zsh
Variables Local and Global
Linux’s environment variables can be divided into local and worldwide variables, as with many other programming languages. A global variable in Python, for instance, can be used everywhere in the program. A local variable, on the other hand, is defined in a function that can only be used in that function.
An environment variable in Linux can also be local. This means it is only accessible within the shell that it was initialized. You can, however, access a global variable from all sub shells.
This is why a Jupyter Notebook can be opened in a shell to access the shell variables in a main shell it was started with using the! commands. If you do this:
Next, try to print it using this:
It is clear that the local variable LOCAL_VAR cannot be accessed while the global variable GLOBAL_VAR can.
How to set environment variables
It’s easy to set up environment variables. It all depends on the purpose of setting them up. For example, if you wish to set them up globally (which is the most common use scenario), you would use the export command.
export GLOBAL_VAR="/usr/raha/"
This export statement can be placed in the bash_profile.sh script to set this variable every time you open up your shell. This will ensure that your GLOBAL_VAR is set to /usr/raha/ the next time you open a shell instance.
Software Engineering: More5 Ways to Learn Git & Version Control
How to Set Global Environment Variables
It’s easy to unset a set environment variable. You can simply use the keyword “unset”. You can use the keyword unset to set an environment variable.
Unset LOCAL_VAR
Unset GLOBAL_VAR
To unset them all, you can also use “unset LOCAL_VAR or GLOBAL_VAR”.
What happens if you set a global variable in subshell? Just as you would expect. It sets in the subshell, but it remains in the main shell.
That’s it! Good luck with your variables!