The environment variables are a set of key-value pairs that define the shell environment and can affect the behavior of the ongoing programs that are executed in the current shell session.
The programs running in the current shell session can access the variable values from the shell or subshell, and you can set them for various purposes.
For example, customizing the user preferences, shrinking long commands into short ones, making binary programs accessible by adding their paths to the environment variables, and many more.
Tutorial Details
Description | Environment Variables |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | env, printenv, echo, grep, set, unset, source, export |
Internet Required | No |
Scope of the Environment Variables in Linux
There are two types of environmental variables:
- Global Environment Variable
- Local Environment Variable
Global Environment Variable
These variables are persistent variables that can be accessed from anywhere; you can even use them in your shell script.
These variables with their values are stored either in the “~/.bash_profile
” or “~/.profile
” or “~/.bash_login
” or “~/.bashrc
” files according to the requirement.
Local Environment Variable
These variables are temporary variables, which means they are accessible until the current session is over. After the session is terminated, all the local variables will be flushed.
Common Environment Variables in Linux
The following is a list of known environment variables that you might already be using:
Environment Variables | Description |
---|---|
PATH | It stores the multiple directory absolute paths separated using the “:” colon that are used to look for while executing any command from the terminal. |
USER | It stores the currently logged-in user’s username. |
HOME | It stores the path to the current logged-in user’s home directory. |
EDITOR | It stores the absolute path of the editor that will be used by default. |
UID | It stores the current logged-in user’s UID. |
TERM | Define the terminal type. |
SHELL | It stores the current logged-in user’s default shell absolute path. |
PWD | Display the current working directory. |
LOGNAME | It stores the name of the logged-in user. |
LANG | It stores the default system language. |
Execute the following command to get a complete list of environment variables and their values in your system.
$ env
You can also ignore the values from the output by only printing the environment variables (keys) using the awk command.
$ env | awk -F "=" '{print $1}'
How to Check Existing Environment Variables in Linux
As you learned previously, the env command can help you find your existing environment variables; however, there are a few more ways to list them, followed by:
Commands | Definition |
---|---|
env | It is used to create a fresh set of environment variables or replace the existing ones. If specified without arguments, then it will list the existing environment variables in your system. |
printenv | It will print all the existing environment variables on your system or the value of the specified variable. |
echo $key | Append the “$ ” at the beginning of the variable name (or key) to fetch their value. |
Using the env Command
The env command will list the current environment variables in your system:
$ env
Output:
Using the printenv Command
The printenv command will also list the present environment variables in your system:
$ printenv
Output:
To get the value of the specific variable, specify its name (or key) as an argument to the printenv command:
$ printenv LOGNAME
Output:
Another example,
$ printenv SHELL
Output:
Using the echo $key
Append the “$
” at the beginning of the variable name (or key) to fetch their value using the echo command.
$ echo $LOGNAME
Output:
Another example,
$ echo $SHELL
Output:
Setting Environment Variables
As we discussed earlier, environment variables have multiple scopes: global and local.
We will first take a look at local variables:
Setting Local Environment Variable
The easiest way to set local environment variables is by directly specifying the key-value pair in your terminal, as shown.
$ site=linuxtldr.com
Note that you cannot directly fetch the value of this variable from its variable name using the env or printenv commands.
However, you can use the set command and pipe the grep command along with the key name to get the variable value, as shown.
$ set | grep site
Output:
Note that the above method of creating variables will only print the value in the parent shell where the variable was created.
If you create a sub-shell by running the bash command in your current shell and try to fetch the value of the same variable, you will get null.
#Parent Shell
$ set | grep site
site=linuxtldr.com
#Creating Sub-Shell
$ bash
#Unable to get the value of the variable
$ set | grep site
$ exit
exit
#Exiting Sub-Shell
$ set | grep site
site=linuxtldr.com
Output:
However, you can use the export command while creating the variables to make them accessible inside the sub-shell.
$ export site=linuxtldr.com
The good thing about specifying export at the beginning while creating environment variables is that you are able to fetch variable values using the env or printenv commands.
$ echo $site
$ printenv site
Output:
And you are also able to access the variable values inside the sub-shell.
#Parent Shell
$ printenv site
linuxtldr.com
#Creating Sub-Shell
$ bash
$ printenv site
linuxtldr.com
$ exit
exit
Output:
To remove/delete this variable, specify the variable name (or key) as an argument to the unset command.
$ unset site
Output:
Setting Global Environment Variable
The previous method of creating environment variables will not persist the variable after the session is closed or restarted.
To make it accessible even after that, specify these variables inside your shell configuration file.
But first, you have to find your default shell and its configuration file path.
Execute any of the following commands to find your existing default shell.
$ printenv SHELL
#OR
$ echo $0
Output:
The following is a list of the configuration file locations for known shells.
Shell | Configuration File Location |
---|---|
bash shell | “~/.bash_profile ” or “~/.bash_login ” or “~/.bashrc ” or “~/.profile “ |
zsh shell | ~/.zshrc |
fish shell | ~/. config/fish/config. fish |
~/.bash_profile
” (or “~/.profile
“) if you need them in an interactive login shell (it will be only executed once when you log in to your system); otherwise, add them in “~/.bashrc
” to execute them in an interactive non-login shell (it will be executed on an already logged-on system or when you create a sub-shell).For example, if Bash is your default shell, then edit the “~/.bashrc
” file using your choice of text editor, like Vim or Nano.
$ vim ~/.bashrc
#OR
$ nano ~/.bashrc
And then add your environment variables at the end of the configuration file, as shown.
Save and close the configuration file, then source it to reflect the changes.
$ source ~/.bashrc
Output:
Lastly, test the environment variable by fetching the values using the variable name.
$ echo $site
#OR
$ printenv site
Output:
Note that these variables are also accessible within the sub-shell.
#Parent Shell
$ echo $site
linuxtldr.com
#Creating Sub-Shell
$ bash
$ echo $site
linuxtldr.com
$ exit
exit
Output:
To remove this variable from your system, you need to edit the shell configuration file (referring to the “~/.bashrc
” file) and remove the previously added line, then save and source the configuration file to reflect the changes.
That was all you needed to know about environmental variables.
If you have any questions or queries related to this topic, then feel free to ask them in the comment section.
Till then, peace!
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.