CIS120Linux Fundementals
Understanding the Environment in Linux
The shell maintains a body of information during a session called the environment. Programs use data stored in the environment to determine facts about the system's configuration. While most programs use configuration files to store settings, some also look for values in the environment to adjust their behavior. Knowing this, we can customize our shell experience using the environment.
In this lesson, we will work with the following commands:
printenv
– Print part or all of the environmentset
– Set shell optionsexport
– Export environment to subsequently executed programsalias
– Create an alias for a command
What is Stored in the Environment?
The shell stores two basic types of data in the environment: environment variables and shell variables. Shell variables are bits of data placed there by bash, while 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, you can use either the set
builtin in bash or the printenv
program. The set
command will show both shell and environment variables, while printenv
will only display the latter. Since the list of environment contents can be long, it is best to pipe the output into less
.
Example:
printenv | less
Output:
USER=me
PAGER=less
LSCOLORS=Gxfxcxdxbxegedabagacad
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg
PATH=/home/me/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
DESKTOP_SESSION=ubuntu
QT_IM_MODULE=ibus
QT_QPA_PLATFORMTHEME=appmenu-qt5
JOB=dbus
PWD=/home/me
XMODIFIERS=@im=ibus
GNOME_KEYRING_PID=1850
LANG=en_US.UTF-8
GDM_LANG=en_US
MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.path
MASTER_HOST=linuxbox
IM_CONFIG_PHASE=1
COMPIZ_CONFIG_PROFILE=ubuntu
GDMSESSION=ubuntu
SESSIONTYPE=gnome-session
XDG_SEAT=seat0
HOME=/home/me
SHLVL=2
LANGUAGE=en_US
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
LESS=-R
LOGNAME=me
COMPIZ_BIN_PATH=/usr/bin/
LC_CTYPE=en_US.UTF-8
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/share/gnome:/usr/local/share:/usr/share
QT4_IM_MODULE=xim
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-IwaesmWaT0
LESSOPEN=| /usr/bin/lesspipe %s
INSTANCE=
The output lists environment variables and their values. For example, the variable USER
contains the value me
. The printenv
command can also list the value of a specific variable.
Example:
printenv USER
Output:
me
The set
command, when used without options or arguments, will display both shell and environment variables, as well as any defined shell functions. Unlike printenv
, its output is sorted in alphabetical order.
Example:
set | less
Commonly Used set
Options:
Option | Description |
---|---|
-o |
Set a shell option |
+o |
Unset a shell option |
-u |
Treat unset variables as an error |
-x |
Print commands and their arguments as they are executed |
-v |
Print shell input lines as they are read |
Examples:
To set the shell option to treat unset variables as an error:
set -u
To print commands and their arguments as they are executed:
set -x
To unset these options:
set +u
set +x
To view shell options:
set -o
You can also view the contents of a variable using the echo
command:
echo $HOME
Output:
/home/me
To see aliases, enter the alias
command without arguments:
alias
Output:
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --showdot --show-tilde'
To create an alias, use the alias
command followed by the name you want to assign and the command it represents:
alias l='ls -l'
alias rm='rm -i'
Some Interesting Variables
The environment contains quite a few variables, and while they may differ from system to system, the following are commonly found:
Variable | Contents |
---|---|
DISPLAY |
The name of the display if running a graphical environment, usually :0 . |
EDITOR |
The name of the program to be used for text editing. |
SHELL |
The name of the user’s default shell program. |
HOME |
The pathname of your home directory. |
LANG |
Defines the character set and collation order of your language. |
OLDPWD |
The previous working directory. |
PAGER |
The name of the program to be used for paging output, often /usr/bin/less . |
PATH |
A colon-separated list of directories that are searched when you enter the name of an executable program. |
PS1 |
This stands for "prompt string 1" and defines the contents of the shell prompt. |
PWD |
The current working directory. |
TERM |
The name of your terminal type. |
TZ |
Specifies your time zone. |
USER |
Your username. |
How Is The Environment Established?
When you log on to the system, bash 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 your home directory that define your personal environment. The exact sequence depends on the type of shell session being started: login or non-login.
Login Shell Session: A session where you are prompted for your username and password, such as when starting a virtual console session.
Non-Login Shell Session: Typically occurs when launching a terminal session in the GUI.
Startup Files for Login Shell Sessions:
File | Contents |
---|---|
/etc/profile |
A global configuration script that applies to all users. |
~/.bash_profile |
A user's personal startup file 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. |
Startup Files for Non-Login Shell Sessions:
File | Contents |
---|---|
/etc/bash.bashrc |
A global configuration script that applies to all users. |
~/.bashrc |
A user's personal startup file to extend or override settings in the global configuration script. |
Non-login shells inherit the environment from their parent process, usually a login shell.
The ~/.bashrc
file is important because it is almost always read. Non-login shells read it by default, and most startup files for login shells are written to read the ~/.bashrc
file as well.
What's in a Startup File?
A typical .bash_profile
might look like this:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
Lines starting with #
are comments. The fourth line contains an if
compound command:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
This means: If the file ~/.bashrc
exists, then read it. This is how a login shell gets the contents of .bashrc
.
The next part deals with the PATH
variable:
PATH=$PATH:$HOME/bin
This modifies PATH
to add $HOME/bin
to the end of the list of directories searched when a command is entered. This allows you to create a bin
directory in your home directory for storing private programs.
Finally:
export PATH
The export
command makes the contents of PATH
available to child processes of the shell.
Modifying the Environment
You can customize your environment by modifying the startup files.
Which Files Should You Modify?
- Add directories to your
PATH
or define additional environment variables in.bash_profile
(or.profile
for some distributions like Ubuntu). - For other customizations, use
.bashrc
.
Text Editors: To modify shell startup files and configuration files, use a text editor. Linux systems typically come with several text
editors, both graphical (like gedit for GNOME or kate for KDE) and text-based (like nano, vi/vim, and emacs).
For this class we will be using vi/vim.