Learn Linux 11: The Environment

Published Jan 12, 2022



The shell maintains a body of information during our shell session called the environment. Programs use the data stored in the environment to determine facts about the system’s configuration. This chapter will introduce the following commands:

  • printenv - Print part or all of the environment
  • set - Set shell options
  • export - Export environment to subsequently executed programs
  • alias - Create an alias for a command

What Is Stored In The Environment?

The shell stores two basic types of data in the environment; though, with bash, the types are largely indistinguishable. They are environment variables and shell variables. Shell variables are bits of data placed there by bash, and environment variables are everything else. In addition to variables, the shell stores some programmatic data, namely, aliases and shell functions.

Examining The Environment

To see what is stored in the environment, we can use either the set builtin in bash or the printenv program. The set command, when used without options or arguments, will display both the shell and environment variables, as well as any defined shell functions. Unlike printenv, its output is courteously sorted in alphabetical order.

[user@linux ~]$ printenv | less
[user@linux ~]$ set | less

Some Interesting Variables

Here is a list of interesting environment variables:

  • USER - Your username.
  • PWD - The current working directory.
  • SHELL - The name of your shell program.
  • OLDPWD - The previous working directory.
  • HOME - The pathname of your home directory.
  • EDITOR - The name of the program to be used for text editing.
  • LANG - Defines the character set and collation order for your language.
  • PS1 - Stands for "prompt string 1." This defines the contents of the shell prompt.
  • DISPLAY - The name of your display if you are running a graphical environment. Usually this is :0, meaning the first display generated by the X server.
  • PAGER - The name of the program to be used for paging output. This is often set to /usr/bin/less.
  • PATH - A colon-separated list of directories that are searched when you enter the name of a executable program.
  • TERM - The name of your terminal type. Unix-like systems support many terminal protocols; this variable sets the protocol to be used with your terminal emulator.
  • TZ - Specifies your time zone. Most Unix-like systems maintain the computer’s internal clock in Coordinated Universal Time (UTC) and then display the local time by applying an offset specified by this variable.

How Is The Environment Established?

When we log on to the system, the bash program starts and reads a series of configuration scripts called startup files, which define the default environment shared by all users. This is followed by more startup files in our home directory that define our personal environment. The exact sequence depends on the type of shell session being started. There are two kinds.

  • A login shell session. This is one in which we are prompted for our username and password. This happens when we start a virtual console session, for example.
  • A non-login shell session. This typically occurs when we launch a terminal session in the GUI.

Here is a list of startup files for login shell sessions:

  • /etc/profile - A global configuration script that applies to all users.
  • ~/.bash_profile - A user’s personal startup file. It can be used to extend or override settings in the global configuration script.
  • ~/.bash_login - If ~/.bash_profile is not found, bash attempts to read this script.
  • ~/.profile - If neither ~/.bash_profile nor ~/.bash_login is found, bash attempts to read this file. This is the default in Debian-based distributions, such as Ubuntu.

Here is a list of startup files for non-login shell sessions:

  • /etc/bash.bashrc - A global configuration script that applies to all users.
  • ~/.bashrc - A user’s personal startup file. It can be used to extend or override settings in the global configuration script.

What Is In A Startup File?

Here is the inside of a typical .bash_profile.

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
  . ~/.bashrc

# User specific environment and startup programs
export PATH

Lines that begin with a # are comments and are not read by the shell. The first interesting thing occurs on the fourth line which says "if the file ~/.bashrc exists then read the ~/.bashrc file". The next thing in our startup file has to do with the PATH variable. Ever wonder how the shell knows where to find commands when we enter them on the command line? For example, when we enter ls, the shell does not search the entire computer to find /bin/ls (the full pathname of the ls command); rather, it searches a list of directories that are contained in the PATH variable. On the ninth line PATH is modified to add the directory $HOME/bin to the end of the list. By adding the string $HOME/bin to the end of the PATH variable’s contents, the directory $HOME/bin is added to the list of directories searched when a command is entered.

Lastly, we have export PATH. The export command tells the shell to make the contents of PATH available to child processes of this shell.

Modifying The Environment

Because we know where the startup files are and what they contain, we can modify them to customize our environment. As a general rule, to add directories to your PATH or define additional environment variables, place those changes in .bash_profile (or the equivalent, according to your distribution; for example, Ubuntu uses .profile). For everything else, place the changes in .bashrc.

The changes we have made to our .bashrc will not take effect until we close our terminal session and start a new one because the .bashrc file is read only at the beginning of a session. However, we can force bash to reread the modified .bashrc file with the following command.

[user@linux ~]$ source ~/.bashrc


In this chapter we learned about the environments, where they are stored, and how to modify them.