UNIX Tutorial Eight

8.1 UNIX shells

The Unix shell is the program that interprets the commands the user types in calls the kernel to carry them out (see the UNIX Introduction). Many shells have been developed for Unix over the years — the Bourne Shell, Bourne Again Shell, C Shell, Korn Shell, etc. The default shell for OS X is the Bourne Again Shell or BASH. Type

$ echo $SHELL

to see which shell you are currently using. It should be BASH.

8.2 Environment Variables

$SHELL is an example of an environment variable. Environment variables are a way of passing information from the shell to programs when you run them. Every program runs in its own environment. Programs look "in the environment" for particular variables and if they are found will use the values stored. Some of these environment variables are set by the system, others by you, yet others by the shell, or any program that loads another program.

Standard UNIX environment variables have a far reaching significance, and those set at login are valid for the duration of the session. By convention, environment variables have UPPER CASE names.

Another example of an environment variable is the OSTYPE variable. The value of this is the current operating system you are using. Type

$ echo $OSTYPE

More examples of environment variables are

Finding out the current values of these variables.

ENVIRONMENT variables are set using the export command, and displayed using the printenv or env commands.

To show all values of these variables, type

$ env | less

Suppose you wanted to set the $PRINTER environment variable to Physics262. You do this by typing

$ export PRINTER=Physics262

To check that it is set type

$ echo $PRINTER

Notice that when you set a variable you just type the variables name, but to reference a variable you need to include a leading '$'.

Environment variables are sometimes called global variables because any child processes that are spawned from the shell after a global variable has been set are inherited by the child processes. You can define a local variable by typing

$ newvar=foobar

The above command set a local variable call newvar equal to the string 'foobar'. Since newvar is defined without the using the export command it is local and won't be passed to child processes.

8.3 Setting the PATH

When you type a command, the environment PATH variable defines in which directories the shell will look to find the command you typed. If the system returns a message saying "command: Command not found", this indicates that either the command doesn't exist at all on the system or it is simply not in your path.

For example, to run units, you either need to directly specify the units path (~/units174/bin/units), or you need to have the directory ~/units174/bin in your path.

You can add it to the end of your existing PATH (the $PATH represents this) by issuing the command:

$ export PATH="$PATH:~/units174/bin"

Test that this worked by trying to run units in any directory other that where units is actually located.

$ cd; units

HINT: You can run multiple commands on one line by separating them with a semicolon.

8.4 The .profile file

Each time you login to a UNIX host, the system looks in your home directory for initialization files. Information in these files is used to set up your working environment. The BASH shell uses a file called .profile (note that the file name begins with a dot). The .profile file can contain any set of Unix commands. The shell reads the file and executes each command in order. The .profile file is the place to define any environment variables that you want set whenever you start an new shell.

By default, the .profile file doesn't exist in your home directory. It is a simple text file so you can create it if needed using any text editor.

WARNING: NEVER put commands that run graphical displays (e.g. a web browser) in your .profile file.

The .profile is a good place to set the PATH environment variable to include paths that aren't included by default. In particular the directory /usr/local/bin isn't included in the default PATH by OS X.

Exercise 8a

Check to see if your system has a /usr/local/bin directory and find out what is in it by typing

$ ls /usr/local/bin

If the directory exits, use a text editor to create a .profile file in your home directory and use the export command to add the directory to your path (export PATH="$PATH:/usr/local/bin"). Type

$ . .profile

to execute your new .profile file. Now try to run one of the commands in /usr/local/bin to make sure it worked. Now whenever create a new Terminal window, the .profile file will be executed and /usr/local/bin will be added to your path.

NOTE: Applications that you run under X11, including xterm, don't run .profile. You can do it by hand entering . .profile. An alternative is to create a .bashrc file. .bashrc is like .profile and is executed by both X11 and the Terminal application whenever you run a new shell.

Adapted for Mac OS X by Shane Burns from a tutorial by M.Stonebank@surrey.ac.uk October 2001