Linux Tutorial Eight

8.1 Linux 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 Linux 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; 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 the setenv command, displayed using the printenv or env commands, and unset using the unsetenv command.

To show all values of these variables, type

$ printenv | less

8.3 Shell Variables

An example of a shell variable is the HISTSIZE 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. Type

$ echo $HISTSIZE

More examples of shell variables are

Finding out the current values of these variables.

SHELL variables are both set and displayed using the set command. They can be unset by using the unset command.

To show all values of these variables, type

$ set | less

8.4 Using and setting variables

Each time you login to a Linux host, the system looks in your home directory for initialisation files. Information in these files is used to set up your working environment. The C and TC shells uses two files called .login and .cshrc (note that both file names begin with a dot).

At login the C shell first reads .cshrc 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 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.

WARNING: We strongly recommend that you do not change the default environment by modifying these files. Instead, set the environment by creating custom scripts and using the source command. Source the files only when necessary, such as before compilation or for running a job.

8.6 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 the executable you compiled in Part 7, 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:

$ set 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.

Acknowledgements

M.Stonebank@surrey.ac.uk October 2001