In Linux based systems you can set dynamic named values as environment variables. These values are stored within system and that are used by shells. An environment variable is a variable with a name and an associated value. In this article, we will show you how to set and use environment variables.
Environment and Shell Variables
The basic format for the variable are as following:
- Environment variables names should in
UPPER CASE. Remember that the names of the variables are case-sensitive.
- To assign multiple values you should use
:colon as separator.
- There should not have space before or after the
There are two types of variables, Environment variable and Shell variable:
- Environment variables are variables those are available system-wide and are inherited by all spawned child processes and shells.
- Shell variables are variables that apply only to the current shell instance. Each shell such as
bash, has its own set of internal shell variables.
List Environment and Shell Variables
Following commands are available that allow you to list
environment variables in Linux:
env– This command allows you to run another program in a custom environment without modifying the current one. It will print a list of the current
environment variableswhen used without an argument.
printenv– The command prints the specified
set– It will set or unset shell variables. It will list all the variables including
shellvariables, when used without an argument.
unset– Used to delete shell and
export– The command sets
printenv is mostly used to print the environment variables. If any argument is passed then it will print only value of the variable otherwise it will print a list of all environment variables.
For instance, to print the value of the
USER environment variable, you would type:
It will show output as following:
You can print multiple variables simultaneously separating by space:
printenv USER HOME
If you run the
env command without any argument then it will show a list of all the variables.
LESSCLOSE=/usr/bin/lesspipe %s %s
SSH_CLIENT=184.108.40.206 6366 22
LESSOPEN=| /usr/bin/lesspipe %s
Following are some of the most common environment variables:
USER– It will show the current logged in user.
HOME– It’s home directory of the current user.
EDITOR– It will show name of default file editor.
SHELL– Display the path of the current user’s shell, such as
LOGNAME– The name of the current user.
PATH– It is a list of directories. When you run a command the system will search those directories in this order and use the first found executable.
LANG– The current locales settings.
TERM– The current terminal emulation.
To get the list of all variables and shell functions you should use
The above command will show a large list in output so you can pipe the output to the less command:
set | less
Set Shell and Environment Variables
To create a new shell variable with name
FIRST_VAR and set
TecNStuff, you would type:
Verify that variable is set using
echo $FIRST_VAR or you can filter output using grep command:
Now we will use printenv command to check whether the variable is environment or shell variable.
The output is blank, that means it’s not a environment variable.
In this article, we have shown you how to set and list
If you have any question or feedback, feel free to leave comment below.