UNIX Tutorial Eight

8.1 UNIX Variables

Variables are a way of passing information from the shell to programs when you run them. Programs look "in the environment" 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.

Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; while environment variables have a farther reaching significance, and those set at login are valid for the duration of the session. By convention, environment variables have UPPER CASE and shell variables have lower case names.

8.2 Environment Variables

An 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 either the setenv command or the export command, depending on your login shell (see section 8.3 below). ENVIRONMENT variables are displayed using the printenv or env commands. They may be unset using either the unsetenv command, or the export command with no arguments.

To show all values of these variables, type

% printenv | less

Or to show only a selected variable, you may use the echo command, followed by the variable you wish to view. For example, find out your type of login shell with the command

% echo $SHELL

8.3 Shell Variables

An example of a csh or tcsh shell variable is the history variable. The value of this is how many shell commands to save, allow the user to scroll back through all the commands they have previously entered. In the csh or tcsh shells, type

% echo $history

The corresponding bash shell variable is the HISTSIZE variable, which may be viewed by typing

% echo $HISTSIZE

More examples of shell variables in csh and tcsh are

Additional examples of shell variables in bash are

Finding out the current values of these variables.

SHELL variables are displayed using the set command. To show all values of these variables, type

% set | less

8.4 Using and setting variables

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 csh and tcsh shells uses two files called .login and .cshrc (note that both file names begin with a dot), while the BASH shell uses the files .login and .bashrc.

At login the C shell first reads .cshrc followed by .login. Similarly, the BASH shell first reads .bashrc followed by .login

.login is to set conditions which will apply to the whole session and to perform actions that are relevant only at login.

.cshrc or .bashrc is used to set conditions and perform actions specific to the shell and to each invocation of it.

The guidelines are to set ENVIRONMENT variables in the .login file and SHELL variables in the .cshrc file, though any of these can be located in either file.

WARNING: NEVER put commands that run graphical displays (e.g. a web browser) in your .cshrc, .bashrc or .login file.

8.5 Setting shell variables in the .cshrc file

For example, to change the number of shell commands saved in the history list, you need to set the shell variable history. It is set to 100 by default, but you can increase this if you wish.

% set history = 200

Check this has worked by typing

% echo $history

However, this has only set the variable for the lifetime of the current shell. If you open a new xterm window, it will only have the default history value set. To PERMANENTLY set the value of history, you will need to add the set command to the .cshrc file.

First open the .cshrc file in a text editor. An easy, user-friendly editor to use is emacs.

% emacs ~/.cshrc

Add the following line AFTER the list of other commands.

set history = 200

Save the file and force the shell to reread its .cshrc file buy using the shell source command.

% source .cshrc

Check this has worked by typing

% echo $history

8.6 Setting shell variables in the .bashrc file

To change the number of shell commands saved in the history list, you need to set the shell variable history. It is set to 100 by default, but you can increase this if you wish.

% export HISTSIZE = 200

Check this has worked by typing

% echo $HISTSIZE

However, this has only set the variable for the lifetime of the current shell. If you open a new xterm window, it will only have the default history value set. To PERMANENTLY set the value of history, you will need to add the set command to the .bashrc file.

First open the .bashrc file in a text editor. An easy, user-friendly editor to use is emacs.

% emacs ~/.bashrc

Add the following line AFTER the list of other commands.

export HISTSIZE = 200

Save the file and force the shell to reread its .bashrc file buy using the shell source command.

% source .bashrc

Check this has worked by typing

% echo $HISTSIZE

8.7 Setting the path

When you type a command, your path (or 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 either the command:

% set path = ($path ~/units174/bin)

in the C or TC shells, or in the BASH shell with 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

To add this path PERMANENTLY, add the following line to your .cshrc AFTER the list of other commands,

set path = ($path ~/units174/bin)

or to your .bashrc AFTER the list of other commands

export PATH=$PATH:~/units174/bin

M. Stonebank, October 2001,     D.R. Reynolds, 13 January 2009