Register to get access to free programming courses with interactive exercises

Environment variables CLI fundamentals

Remember that the command cd with no arguments makes the user's home directory the working directory. You may ask how it knows where the home directory is. You may assume that the cd command somehow accesses the operating system and finds out the necessary information from it, or looks directly in the configuration files. But in fact, it doesn't do either. This command orients itself using a so-called environment variable. Environment variables are similar to variables in programming languages. They exist within a running shell session and are loaded there during its initialization (but this is not the only way they can appear). You can see the set variables using the env (environment) command. Below is a partial list of these variables that are available on one of the Hexlet servers.

env

TERM=xterm-256color
SHELL=/bin/bash
LC_ALL=en_US.UTF-8
USER=kirill.m
HEXLET_VERSION=v2711
PATH=/home/kirill.m/bin:/home/kirill.m/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
PWD=/home/kirill.m
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/kirill.m
LOGNAME=kirill.m

The output format is very simple: to the left of the equal sign is the variable name, and to the right is the value. The env output may differ for different OS not only in the values of these variables but also in their composition. For example, you definitely won't have HEXLET_VERSION, but you will probably have HOME, only with a different value.

The main purpose of environment variables is to configure the system and programs. Their convenience lies in their universality and the fact they are not linked with the way these values appear in the system. Let's take cd again. This command only knows that the home directory is available in the HOME environment variable. How it got into the system is unimportant. Without the environment variable, you would have to do one of two things:

  • Each time when calling cd, you'd have to specify where the home directory is located. For example: cd --home-dir /home/kirill.m. But this way defeats the whole point of a quick switch to the home directory
  • Arrange for a special file to be stored somewhere with the settings (including the home directory) to be read by the cd command each time it is run

There is a basic set of variables that are always set by bash at startup. They are used by a large number of utilities and are necessary for system to function normally. One of these variables we already know is HOME.

# echo is used to output the variable,
# $ sign goes in front of the variable itself.
echo $HOME

/home/kirill.m

# There must be no spaces around the equal sign
HOME=/tmp
echo $HOME

/tmp

# Back again
HOME=/home/kirill.m
echo $HOME

/home/kirill.m

Now let's experiment: try to set HOME and navigate to this directory with the cd command, which should send us to the home directory.

# It's enough to assign a value to the left of the command to be run
HOME=/tmp cd

/tmp

pwd

/tmp

The cd command jumped to the path specified in the HOME variable. Now we do cd again, but without adding the variable definition on the left.

cd
pwd

/home/kirill.m

echo $HOME

/home/kirill.m

This time it moved to the actual home directory. It turns out that calling HOME=/tmp cd only changed the variable for that particular run. Indeed, there are two ways to set the value of an environment variable: local and global. When we specified HOME=/tmp just before the cd command, the variable was not changed for the current session. It was changed for the command that was run, in this case, cd. This trick can be done with any command. The other way of setting an environment variable globally is as follows:

echo $HOME

/home/kirill.m

export HOME=/tmp
cd
pwd

/tmp

echo $HOME

/tmp

Note that the change takes place within the current session, in other words, if you have two terminal tabs open and bash is loaded in each, the change will only take place in the tab in which the command is executed.

The PATH variable plays a special role among the environment variables. It's so important to developers, and there are so many difficulties associated with it, that there's practically a whole lesson ahead about this variable.


Do it yourself

Examine the environment variables in your local environment by typing env

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY