Unix: Customising the environment

Shell variables

Variables are a way of passing information from the shell to programs when you run them. Programs look for particular variables and if they are found will use the values stored. Some are set by the system, others by you, yet others by the shell, or any program that loads another program.

Shell variables are come in two flavours: environment variables and local variables. They both work the same way; the only difference lies in what happens when some script runs another script or program in a sub-process (which, of course, they do all the time). Environment variables are passed on to sub-processes. Local variables are not.

Local variables apply only to the current instance of the shell and are used to set short-term working conditions. Environment variables are usually set at log-in, and are valid for the duration of the session.

By convention, environment variables have UPPER CASE names. Some prefer to use lower case names for local variables to make them distinct from environment variables.

Environment Variables

An example of an environment variable is the OSTYPE variable. The value of this is the current operating system you are using. To see the current value of OSTYPE, type:

$ echo $OSTYPE

More examples of environment variables are:

To show all currently defined environment variables and their values, type:

$ env

Note that since env runs a sub-process, no local variables will appear.

To set a new variable, or assign a new value to an existing, use the following format:

$ VARIABLE=value
$ export VARIABLE

The export command is what makes it an environment variable. Without exporting, only the shell will know the variable, and it will be local.

Local Variables

These are local to scripts. xxxx

Initialisation files

Each time you log-in to a Unix machine, or when you start up another shell without logging in, the system looks both in the global /etc directory and in your home directory for initialisation files. Commands in these files are used to set up your working environment.

How this is done varies. Some system administrators use the initialisation files to enforce certain site-wide rules. If you are using Unix on a University or a departmental computer, there may exist local guidelines about initialisation files.

On a typical Unix system, the shell (if it is bash) reads at least one of two files that reside in your home directory called .profile and .bash (note that both file names begin with a dot) to initialise the environment. These files often have embedded guidelines about usage, so reading them may be a good idea.

If the shell is interactive (i.e. not a script) and not a log-in shell, bash inherits the environment exported parent process and reads .bashrc.

If the shell is an interactive log-in shell (no parent shell) bash reads .profile to establish the initial environment. Some users invoke .bashrc as the last line in .profile.

Use .profile is to set conditions which will apply to the whole session and to perform actions that are relevant only at log-in. Use .bashrc is used to set conditions and perform actions specific to the shell and to each invocation of it.

A shell script

Shell programming is beyond the scope of this ebook. But to demonstrate how the environment value PATH is used by the shell to search for executable files, we need a shell script. To create one, fire up your favourite Unix editor (or nano if you don't have one yet), and create a file with the following five lines:

#!/bin/sh
echo "Hello, $USER!"
echo "The date and time now are: "
date
# EOF

Or you can download it from here.

Save this as a file named greeting in your exercises directory, and make sure the execute bit is set:

$ chmod a+x greeting

Setting the path

When you type a command, the PATH variable defines in which directories the shell will look to find the command you typed. If the system returns a message saying “-bash: command not found”, either command does not exist at all on the system, or it is not in your path.

For example, to run the greeting script from a different directory, you either need to directly specify the greeting path (~/exercises/greeting), or you need to have the directory where it exists (i.e. ~/exercises) in your path.

You can append ~/exercises to the end of your existing path (i.e. $PATH) by issuing the command:

$ PATH=$PATH:~/exercises

Test that this worked by going to your home directory and give the command greeting there:

$ cd
$ greeting
Hello, john!
The date and time now are: 
Wed Oct 17 08:16:31 CEST 2012

To add this path permanently to your environment, you need to add the following two lines to the end of your .profile:

PATH=$PATH:~/exercises
export PATH