The Linux Environment


These are my notes on the environment in linux.

My book on Cloud Computing with Amazon. It is only $0.99 and buying it helps support my family and I. Thankyou in advance.



The shell holds information about our session and this is called the
environment. Programs use this information to determine our system
configuration. Many programs will use configuration files to store program data
but they will also look to the environment.

Information 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 and environment variables are everything else. In addition to variables,
the shell stores some programmatic data like aliases and shell functions.

To see what is stored in the environment, we can use either the set builtin in
bash or the 'printenv' program. The set command will show both the shell and
environment variables, while 'printenv' will display only the latter. Because
the list of environment contents will be fairly long, it is best to pipe the
output of either command into less.

printenv | less

What we see is a list of environment variables and their values. For example, we
see a variable called user, which contains the value 'me'. The 'printenv'
command can also list the value of a specific variable.

printenv user

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 nicely sorted in alphabetical order.

set | less

It is also possible to view the contents of a variable using the echo command:

echo $HOME

One element of the environment that neither 'set' nor 'printenv' displays is
aliases. To see them, enter the alias command without arguments.

alias

You will see all the defined aliases in your environment.

The environment contains quite a few variables, and though the environment will
differ from system to sytem, we will likely see the most common variables.

Environment
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.

There is 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. There is also a non-login shell session. This typically occurs when we
launch a terminal session in the GUI. login shells read one or more startup

files:

/etc/profile
~/bash.login
~/.profile

Non-login shell sessions read these startup files:


/etc/bash.bashrc

~/.bashrc

In addition to reading the startup files, non-login shells inherit the
environment from their parent process, usually a login shell. Take a look and
see which of these startup files are installed. Most are hidden so we will need
to use the '-a' option when using the 'ls' command. 

The ~/.bashrc file is probably the most important startup file from the ordinary
user's point of view, because it is almost always read. non-login shells read it
by default, and most startup files for login shells are written in such a way as
to read the ~/.bashrc file as well.

Startup Files
Lines that begin with a # are comments and are not read by the shell. These are
there for human readability. The first interesting thing occurs below:

if [-f ~/.bashrc]; then
    . ~/.bashrc
fi

This is called an 'if' compound command. It is saying that fi there is a
~/.bashrc file then read it. 

We can see that this bit of code is how a login shell gets the contents of
.bashrc. The next thing in the startup file has to do with the 'path' variable.
The 'path' variable tells the shell where to find commands when we enter them on
the command line. The 'path' variable is often set by the /etc/profile startup
file with this code:

PATH=$PATH:HOME/bin

PATH is modified to add the directory $HOME/bin to the end of the list. Many
distributions provide this PATH setting by default. 

We also have this command to 'export' our path.

export PATH

The export command tells the shell where the startup files are and what they
contain, we can modify them to customize our environment.

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. For everything else, place the changes in .bashrc.

Unless you are the system administrator and need to change the defaults for all
users of the system, restrict your modifications to the files in your home
directory. It is certainly possible to change the files system wide but it is
safer to not do so.

Text Editors
To edit the shell's startup files, as well as most of the other configuration
files on the system, we use a program called a text editor. A text editor is a
program that allows us to edit words on the screen. It differs from a word
processor by only supporting pure text and often contains features designed for
writing programs. Text editors are the central tool used by software developers
to write code and by system administrators to manage the configuration files
that control the system.

A lot of different text editors are available for linux so most systems have a
few installed by default. Text editors fall into two basic categories, graphical
and text-based. 

There are many text-based editors. The popular ones we will encounter are nano,
vi, and emacs. The nano editor is a simple easy to use editor designed as a
replacement for the pico editor. The vi editor has mostly been replaced by vim,
is the traditional editor for unix and linux systems. The emacs editor is an
all-purpose programming environment installed on most linux systems by default. 

Using a Text Editor
Text editors can be invoked from the command line by typing the name of the
editor followed by the name of the file you want toe dit. If the file does not
already exist, the editor will assume that we want to create a new file.

vim filename

This command will start the vim editor and load the file named 'filename'.

Graphical text editors are pretty self-explanatory, think word processor but
just for plain text. Programmers do not want a word processor because it would
not work well for programming and could mess up configuration files with its
formatting of text. 

Comments in Files
Whenever you modify configuration files, it is a good idea to add comments to
document your changes. shell scripts and bash startup files use a # symbol to
begin a comment. Other configuration files could use a different symbol but they
do the same thing. 

You will often see lines in configuration files that are commented out to
prevent them from being used by the affected program. This is done to give the
reader suggestions for possible configuration choices or examples of correct
configuration syntax. 

Activating Our Changes
The changes we have made to our .bashrc or other configuration files 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:

source ~/.bashrc

After doign this, we should be able to see the effect of our changes.