When a Shell is running there are three types of variables present: In this tutorial, we will see how you can set Environment and Shell variables in Linux.

What are Shell Variables?

The Shell variables are specific to the current shell and used by the Shell to function correctly. These variables are temporary, and to make them permanent you can export them as environment variables. Some of the common shell variables are:

UID: Current logged in user’s ID HOSTNAME: The hostname of the computer at a particular time BASH_VERSINFO: Machine-readable form of bash version BASH_VERSION: Human-readable output of bash version DIRSTACK: Stack of directories available with ‘popd’ and ‘pushd’ command SHELLOPTS: Shell options can be set

What are Environment Variables?

Environment Variables are system-wide available variables that are available to any program or child process of the shell. Also, the Shell Script defines the environment variables that are needed to run the program. Some of the common Environment variables are:

MAIL: Mail directory of the user or the path to user’s mailbox TEMP: Temporary files’ director location PWD: Current working directory OLDPWD: The previous working directory USER: The current user logged in LANG: The current language LOGNAME: User Name HOME: The home directory of the current user _: Recent previous command executed SHELL: The current shell EDITOR: The default text editor of Linux HOSTNAME: The hostname of the device on the network

Here are some of the commands that will help you in setting Shell and Environment variables:

env: You can use a custom environment to run another program without modifying the existing environment. printenv: This will print all the environment variables of your system. set: Used to set the environment and shell variables. unset: Used to delete the environment and shell variables. exports: Let’s you export shell variables into environment variables.

How to print Shell and Environment variables?

Shell keeps a track of all Environment and Shell variables. And to access that we can use these two commands:

printenv: to print all the Environment variables. set: to print Shell variables.

How to set Environment and Shell variables in Linux?

So now we know that what Environment and Shell variables are. So let’s learn how to create and set these variables in Linux. To clearly understand the difference between Environment and Shell variables practically. We will first set a Shell variable and then the Environment variable.

Creating the Shell variable

To set a Shell variable run this command in your Shell. This will create a Shell variable that will be available in your current session.TESTVAR=‘Hello!’ We can also check our variable with the grep command.set | grep TESTVAR You will see an output like this.TESTVAR=‘Hello!’ You can also see the value of a shell variable with the following command.echo $TESTVAR As it is a Shell variable, so it will not be available for other applications or child processes. You can even verify that it is not an Environment variable.printenv | grep TESTVAR There will be no output. It means it is not an Environment variable.

Creating the Environment Variables

Now, let’s export the Shell variable into an Environment variable. Use the following command to do that.export TESTVAR

This will turn our Shell variable into an Environment variable and to verify that run the following command.printenv | grep TESTVAR This time you will see an output like this.TESTVAR=‘Hello!’

Now we know that how we can convert a Shell variable into an Environment variable but how to create an Environment variable directly. You can set an Environment variable in a single step with this command.export NEWVAR=“Hello Env”

It will be directly exported as an Environment variable and you can verify that.printenv | grep NEWVAR You will see an output like this.NEWWAR=Hello Env

This way you can set Shell and Environment in Linux.

Unsetting variables

Now you know that how to set variables. Let’s learn about unsetting them.

You can change an Environment variable into a Shell variable again with this command.export -n TESTVAR It will remain a Shell variable but not an Environment variable. Let’s verify that.printenv | grep TESTVAR There will be no output but if we check it for the Shell variable,set | grep TESTVAR you will see the output.TESTVAR=‘Hello!’ And if you want to completely unset that then use this command.unset TESTVAR


Environment and Shell variables can be very useful for you if you often work with Shell and learning how to set and unset these variables will be good from a long-term perspective.