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 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 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 MYVAR=linoxide.com $ echo $MYVAR linoxide.com $ env | grep MYVAR MYVAR=linoxide.com
Common environment and shell variables
Some environment and shell variables are very useful and are often referenced. Here are some common environment variables:
|the term||This 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.|
|user||The currently logged in user.|
|disabled||The current working directory.|
|Low-density lipoprotein||Previous working directory. This file is retained by the shell to change back to the previous directory by running cd-.|
|LS_COLORS||This 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.|
|The path of the current user’s mailbox.|
|path||A 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.|
|Lang||Current language and localization settings, including character encoding.|
|Family||The home directory of the current user.|
|_||The latest previously executed command.|
In addition to these environment variables, some shell variables you often see are:
|lottery||A list of options used when executing bash. This is useful for determining whether the shell environment will behave the way you want.|
|BASH_VERSION||A version of bash executed in human-readable form.|
|BASH_VERSINFO||The bash version, in machine-readable output.|
|Column||The number of wide columns used to draw the output on the screen.|
|Site||Directory stacks available for pushd and popd commands.|
|File size||The number of command history lines stored in the file.|
|history record||The number of command history lines allowed in memory.|
|CPU name||The host name of the computer at this time.|
|IFS||Internal field separator used to separate input on the command line. This is a space by default.|
|PS1||Main 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.|
|shell||Shell options that can be set using the set option.|
|UID||The 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 SSH_CONNECTION=10.0.2.2 37182 10.0.2.15 22 LESSCLOSE=/usr/bin/lesspipe %s %s LANG=C.UTF-8 XDG_SESSION_ID=5 USER=vagrant MYVAR=linoxide.com PWD=/home/vagrant HOME=/home/vagrant SSH_CLIENT=10.0.2.2 37182 22 XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop SSH_TTY=/dev/pts/0 MAIL=/var/mail/vagrant TERM=xterm-256color SHELL=/bin/bash SHLVL=1 LOGNAME=vagrant XDG_RUNTIME_DIR=/run/user/1000 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin LESSOPEN=| /usr/bin/lesspipe %s _=/usr/bin/printenv
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 /bin/bash $ printenv HOME /home/vagrant $ printenv MYVAR linoxide.com
Using the env command, you can modify the environment in which your program runs by passing a set of variables to the command:
env MYVAR=linoxide.com 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=/bin/bash BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath BASH_ALIASES=() BASH_ARGC=() BASH_ARGV=() BASH_CMDS=() BASH_COMPLETION_VERSINFO=(="2" ="8") BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=(="4" ="4" ="20" ="1" ="release" ="x86_64-pc-linux-gnu") BASH_VERSION='4.4.20(1)-release' COLUMNS=140 DIRSTACK=() EUID=1000 GROUPS=() HISTCONTROL=ignoreboth HISTFILE=/home/vagrant/.bash_history HISTFILESIZE=2000 HISTSIZE=1000 HOME=/home/vagrant HOSTNAME=ubuntu-bionic HOSTTYPE=x86_64 IFS=$' tn' LANG=C.UTF-8 LESSCLOSE='/usr/bin/lesspipe %s %s' LESSOPEN='| /usr/bin/lesspipe %s' LINES=35 LOGNAME=vagrant .....
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 Linoxide
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 Linoxide
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 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.