An environment variable is a dynamic-named value containing information about your login session, set configuration options and customize the shell environment in Linux-based operating systems.
In simple terms, an environment variable is used to define a location for storing a value, you can also refer to with its symbolic name.
These values are stored within the system and are used by command line applications. The stored value can be deleted, displayed, edited and re-saved.
An environment variable gives you information about system behavior. It also allows you to change the behavior of the applications on the system.
In this tutorial, we will show you how to set environment variables in Linux.
Common Environmental and Shell Variables in Linux
There are two types of variables, Environment variables and Shell variable.
1. Environment Variables
These variables are available system-wide and are inherited by all spawned child processes and shells.
2. Shell Variables
These variables are used for only current shell. It is set by the shell and is required by the shell in order to function correctly.
In Linux-based systems, some environment and shell variables are very useful to perform your day-to-day tasks. A brief explanation of commonly used variables are shown below:
- PWD
It displays your current working directory. - USER
It displays your current logged in user. - PATH
It stores the full path of all commands. - LANG
It displays the current language and localization settings. - HOME
It displays the current user’s home directory. - UID
It displays the UID of the current user.
You can see the output of the above variables in the following screen:
List Environment Variables
There are several commands available in Linux that allows you to list and set environment variables.
A brief explanation of each command is shown below:
- printenv
This command prints all or the specified environment variables. - env
This command allows you to run another program in a custom environment without modifying the current one. - set
This command is used to set or unset shell variables. It displays a list of all variables when run without an argument. - unset
This command is used to delete shell and environment variables. - export
This command is used to sets environment variables.
Run the printenv command without any argument will display a list of all environment variables.
printenv
You should see the following screen:
To display the value of PATH and HOME environment variables with the following command:
printenv HOME PATH
You should see the following screen:
The printenv and env command print only the environment variables.
You can get a list of all variables, including environment and shell variables using the set command:
set | less
You should see the following screen:
Setting an Environment Variables
In this section, we will learn how to set shell and environment variables.
To create a shell variable within your current session using the following command:
VAR=webservertalk.com
Next, you can verify the variable is set by using echo command as shown below:
echo $VAR
You should see the following output:
webservertalk.com
You can use the printenv command to check whether this variable is an environment variable or not as shown below:
printenv $VAR
The output will be empty that means the variable is not an environment variable.
You can set Environment variables using the export command.
For example, create a new environment variable using the following command:
export VAR=webservertalk.com
You can now verify the variable is set by using printenv command as shown below:
printenv VAR
You should see the following output:
webservertalk.com
Note: The above environment variables are available only for the current session. If you open a new shell, or if you log out, all variables will be lost.
You can also unset a variable using the unset command as shown below:
unset VAR
Example Screenshot:
Setting Persistent Environment Variables
If you want to set permanent Environment variables, you will need to define those variables in the bash configuration files.
In most Linux-based operating systems, the following files are used to set the permanent environment variables.
/etc/environment
This file is used to set up system-wide environment variables. You can set the variables using this file in the following format:
VAR=webservertalk.com
PATH=/home/user
/etc/profile
Variables set in this file are loaded whenever a bash login shell is entered. You can set the variables using this file in the following format:
export VAR=webservertalk.com
~/.bashrc
This is a user-based configuration file to set environment variables. if you are using Bash, you can declare the variables in the following format:
export VAR=webservertalk.com
You will also run the following command to load the new environment variables into the current shell session:
source ~/.bashrc
Conclusion
In the above tutorial, we learned how to list and set shell and environment variables. These variables are very useful for many programs.