Linux setting and listing environment variables
In Linux and Unix-based systems, an environment variable is a dynamically named set of values that are stored
In Linux and UNIX-based systems, an environment variable is a dynamically named set of values that are stored in the system and used in applications launched by the shell or subshell.
Simply put, an environment variable is a variable with a name and associated value. Environment variables allow you to customize how your system works and how your system applications behave.
For example, environment variables can store information about the default text editor or default browser, the path to an executable file, or system locale and keyboard layout Settings.
In this tutorial, we will explain how to read and set environment variables.
Environment variables and shell variables have a key-value pair format. Variable names are case sensitive. By convention, environment variables should use uppercase characters.
When multiple values are assigned to a variable, they must be separated by the colon: character, such as the PATH environment variable. There is no space around the = symbol.
Variables can be divided into two main categories, environment variables and shell variables.
Environment variables are system-wide available variables that are inherited by the generated shell child processes.
Shell variables are variables that apply only to the current shell session. Each shell program, such as zsh and bash, has its own set of internal shell variables.
Several commands that can be used to list and set environment variables in Linux include env, printenv, set, unset, export.
The env command allows you to customize environment variables in the current shell session.
When used without parameters, it prints a list of the current environment variables.
The printenv command prints all or specified environment variables. The set command sets or deletes shell variables.
When used without parameters, it prints a list of all variables, including environment and shell variables and shell functions.
The unset command removes shell and environment variables. The export command sets environment variables.
Listing environment variables
The most common command for listing environment variables is printenv. If you pass the name of a variable as an argument to the command, only the value of that variable is printed.
If no argument is specified, printenv prints a list of all environment variables, one variable per line.
The printenv HOME
command prints the value of the environment variable for HOME
. It prints out the current logged-in user's path, which is the user's home directory.
You can also pass multiple arguments to the printenv command, such as the command printenv LANG PWD
, which will print the environment variables and the current directory for the language Settings.
If you run printenv or env without any arguments, it displays a list of all environment variables.
printenv #print all environment variables
printenv HOME #print the HOME environment variable
printenv LANG PWD #print multiple environment variables
These are some of the most common environment variables. USER Indicates the current login user. HOME Indicates the home directory of the current user. EDITOR The default text editor to use. This is the editor you will use when editing in the terminal.
SHELL The path to the current user shell, such as bash or zsh. LOGNAME Indicates the name of the current user.
PATH is the list of directories to search for when you run the command. When you run the command, the system searches these directories in that order and uses the first executable found.
LANG Current locale setting. TERM Current emulation terminal. MAIL The location where the current user's mail is stored.
The printenv and env commands print only environment variables. To get a list of all variables, including environment and shell variables and shell functions, use the set command.
The set command does not take any arguments. Command displays a list of all variables, so you may want to pass the output through the pipeline to use less set | less.
You can also print shell variables using the echo command. For example, the command echo $BASH_VERSION prints the value of the variable that BASH_VERSION is to run.
set #print environment variables and shell variables
set | less #View variables in pagination
echo $BASH_VERSION #print shell variables
Setting environment variables
To better illustrate the difference between Shell and environment variables, we'll start by setting Shell variables and then move on to environment variables.
The MY_VAR='myfreax'
command uses the name MY_VAR
and the value myfreax to set a new shell variable, which is not actually a command, such that shell program syntax.
You can use grep to filter the output of the set command to verify that the variable has been set. Command set | grep MY_VAR
will print out the value of the MY_VAR
myfreax.
You can use the printenv command to check if the variable is an environment variable. If the output is empty, it tells us that the variable is not an environment variable.
You can try printing the MY_VAR
variable in a subshell by running the command bash -c 'echo $MY_VAR'
and you will get an empty output.
Because the bash -c command will start a shell session instance, the environment variable set by the current shell session is not inherited by the shell session but inherits the environment variable.
MY_VAR='myfreax'
set | grep MY_VAR
printenv MY_VAR # print empty string
bash -c 'echo $MY_VAR' #print shell variable in subshell, print empty string
The export command is used to set environment variables. The easiest way to export shell variables as environment variables is to use the command export MY_VAR=myfreax
to set MY_VAR as an environment variable.
The command printenv MY_VAR
prints the value of the environment variable MY_VAR, which prints myfreax.
If you try to print environment variables in a child shell, the child shell will inherit the environment variables of the parent shell process.
The bash -c 'echo $MY_VAR'
command starts the subshell session and then prints the MY_VAR environment variable using echo.
At this point, you will print the value of the variable myfreax on the terminal.
You can also set environment variables on a single line, such as the command export MY_NEW_VAR="My New Var"
.
Environment variables created in this way are only available in the current session. If you open a new shell session or if you log out of the current login, all variables are lost.
export MY_VARR=myfreax
printenv MY_VAR #myfreax
bash -c 'echo $MY_VAR' #print environment variable in subshell, print empty string
Persist environment variables
To make environment variables persistent, you need to define them in the bash configuration file.
In most Linux distributions, when you start a new session, the environment variables are read from the following files.
/etc/environment
uses this file to set system-wide environment variables. The variables in this file use the FOO=bar" format to set the environment variables.
/etc/profile
simply logs in to the shell by typing bash to load the variables set in this file. To declare environment variables in this file, you need to set them using the export command.
Each user shell has a specified configuration file. For example, if you use Bash, you can declare variables in the ~/.bashrc
file.
bashrc is a shell script, so when setting environment variables, you need to use the export command, such as export PATH="$HOME/bin:$PATH"
to modify the PATH environment variable.
To load new environment variables into the current shell session, use the source command. The source ~/.bashrc
command will load the variable declared by ~/.bashrc. source can be any file.
In this guide, we show you how to set up and list environment and shell variables. If you have any questions, please feel free to comment.