Set and list environment variables in Linux

In Linux-based systems, you can set dynamically named values ​​as environment variables. These values ​​are stored on the system and used by the command line application. In short, environment variables are variables with names and associated values. A variable is a symbol, letter, or word that represents a value, such as “x” or “y”.

Environment variables can store information about the default text editor or browser, the path to the executable, and more.

Environment variables are globally available in the program and its subroutines. Shell variables are only available in the current shell.

In this tutorial, we will show how to list and set environment variables and shell variables.

Environment variable

Environment variables are system-wide variables and are inherited by all derived child processes and shells.

Environment variables are implemented as strings representing key-value pairs. If multiple values ​​are passed, they are usually separated by a colon (:) character. Each pair usually looks like this:


Variable names are case sensitive and are usually used in uppercase (MYVAR1, MYVAR2 …)

If the value contains spaces, use quotes:

KEY="value with spaces"

Shell variables

Shell variables are variables specifically contained in the shell that sets or defines them. Each shell (such as zsh and bash) has its own set of internal shell variables. They are often used to track temporary data, such as the current working directory. Usage is the same as global environment variables.

To use shell variables as environment variables, use export MYVAR:

$ export
$ echo $MYVAR
$ env | grep MYVAR

Common environment and shell variables

Some environment and shell variables are very useful and are often referenced. Here are some common environment variables:

the termThis specifies the type of terminal to emulate when running the shell. Different hardware terminals can be simulated for different operating requirements. However, you usually don’t have to worry about it.
userThe currently logged in user.
disabledThe current working directory.
Low-density lipoproteinPrevious working directory. This file is retained by the shell to change back to the previous directory by running cd-.
LS_COLORSThis defines a color code for selectively adding color output to the ls command. This is used to distinguish different file types and provide users with more information at a glance.
mailThe path of the current user’s mailbox.
pathA list of directories that the system checks when it looks for commands. When a user types a command, the system checks the executables in the directory in this order.
LangCurrent language and localization settings, including character encoding.
FamilyThe home directory of the current user.
_The latest previously executed command.

In addition to these environment variables, some shell variables you often see are:

lotteryA list of options used when executing bash. This is useful for determining whether the shell environment will behave the way you want.
BASH_VERSIONA version of bash executed in human-readable form.
BASH_VERSINFOThe bash version, in machine-readable output.
ColumnThe number of wide columns used to draw the output on the screen.
SiteDirectory stacks available for pushd and popd commands.
File sizeThe number of command history lines stored in the file.
history recordThe number of command history lines allowed in memory.
CPU nameThe host name of the computer at this time.
IFSInternal field separator used to separate input on the command line. This is a space by default.
PS1Main command prompt definition. This is used to define the appearance of the prompt when starting a shell session. PS2 is used to declare auxiliary prompts when the command spans multiple lines.
shellShell options that can be set using the set option.
UIDThe UID of the current user.

List Shell and Environment Variables

There are several commands available that let you list environment variables in Linux:

env – This command allows you to run another program in a custom environment without modifying the current program. When used without parameters, it will print a list of current environment variables. printenv-This command prints all or specified environment variables. set-This command sets or unsets shell variables. When used without parameters, it will print a list of all variables, including environment and shell variables, and shell functions.

By default, env and printenv should function exactly the same:

$ printenv 
LESSCLOSE=/usr/bin/lesspipe %s %s
SSH_CLIENT= 37182 22
LESSOPEN=| /usr/bin/lesspipe %s

The difference between the env and ‘printenv’ commands is only apparent in their more specific functions. For example, using ‘printenv’, you can request the value of each variable:

$ printenv SHELL
$ printenv HOME
$ printenv MYVAR

Using the env command, you can modify the environment in which your program runs by passing a set of variables to the command:

env command_to_run command_options

The printenv and env commands print only environment variables. If you want to get a list of all variables, including environment and shell variables and shell functions, you can use the set command:

$ set
BASH_VERSINFO=([0]="4" [1]="4" [2]="20" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")
IFS=$' tn'
LESSCLOSE='/usr/bin/lesspipe %s %s'
LESSOPEN='| /usr/bin/lesspipe %s'

The command will display a large list of all variables, so you may want to pass the output to the less command.

set | less

Setting the shell and environment variables

The commands that can be used to set environment variables in Linux are:

set-This command sets or unsets shell variables. When used without parameters, it will print a list of all variables, including environment and shell variables, and shell functions. unset-This command deletes the shell and environment variables. export – This command sets environment variables.

To better understand the difference between shell variables and environment variables, we will start by setting shell variables and then proceed with environment variables.

We will start by defining a shell variable in the current session. You can use echo $ MYVAR to verify that the variable is set

$ MYVAR=Linoxide
$ echo $MYVAR

Use the printenv command to check if this variable is an environment variable:

$ printenv MYVAR

No output should be returned. This means that the MYVAR variable is not an environment variable.

The export command is used to set environment variables.

To create environment variables, simply export the shell variables as environment variables:

$ export MYVAR

You can check this by running:

$ printenv MYVAR

You can also set environment variables in one line:

$ export MYNEWVAR="My New Variable"

Environment variables created in this way are available only for the current session. If you open a new shell or log out, all variables will be lost.

We can also restore environment variables to shell variables and even delete them completely (not set):

Our MYVAR variable is defined as an environment variable. We can change it back to a shell variable by typing:

$ export -n MYVAR

It is no longer an environment variable, but it is still a shell variable.

If we want to unset the shell or environment variables completely, we can use the unset command:

$ unset MYVAR

We can verify that it is no longer set:

$ echo $MYVAR

Because the variable is not set, nothing is returned.

Persistent environment variables

We have already mentioned that many programs use environment variables to determine the specific operation. We don’t want to have to set important variables every time we start a new shell session. The bash shell reads different configuration files depending on how the session was started. An interactive shell session is a shell session connected to a terminal. A non-interactive shell session is a session that is not connected to a terminal session.

The difference between different sessions is whether the shell was generated as a “login” session or a “non-login” session.

In most Linux distributions, when you start a new session, the environment variables are read from the following files:

/ etc / environment -Use this file to set system-wide environment variables.

/ etc / profile -Whenever you enter bash to log in to the shell, the variables set in this file are loaded.

~ / .Bashrc -Shell-specific profiles for each user. For example, if you are using Bash, you can declare variables there.

To load new environment variables into the current shell session, use the source command:

$ source ~/.bashrc

If you need to set a system-wide variable, you may want to consider adding it to / etc / profile, /etc/bash.bashrc, Or / etc / environment.

in conclusion

In this tutorial, we learned how to set and list environment and shell variables. These variables are always present in your shell session and are very useful for many programs. There are many other more common but more common scenarios that you need to read or change the system environment. If you have any questions, feel free to leave them in the comments below.