Loughborough University
Leicestershire, UK
LE11 3TU
+44 (0)1509 222222
Loughborough University

IT Services : High Performance Computing

Linux Tutorial Eight


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 and shell scripts you run within that shell inherit the value of the environment variable set. By convention, environment variables have UPPER CASE and shell variables have lower case names.

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 
      

You will get the result linux-gnu.

More examples of environment variables are

  • USER (your login name)

  • HOME (the path name of your home directory)

  • HOSTNAME (the name of the computer you are using)

  • DISPLAY (the name of the computer screen to display X windows)

  • PATH (the directories the shell should search to find a command)

Finding out the current values of these variables.

ENVIRONMENT variables are set using the export VARIABLE=value command, displayed using the env command, and unset using the unset command.

To show all values of these variables, type

env | less
        

Finding out the current values of variables

The value of a variable can be displayed at the prompt using

echo $VARIABLENAME
      

For example, to show the hostname:

echo $HOSTNAME
      

To show all values of these variables, type

env | less
      

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 shell uses two files called .login and .bashrc (note that both file names begin with a dot).

At login the 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.

.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 .bashrc file.

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

Setting shell variables in the .bashrc 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.

HISTFILESIZE=200
    

Check this has worked by typing

echo $HISTFILESIZE
      

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 gedit (but note it needs X windows so if you are logged onto a system via ssh you may not be able to use this).

gedit ~/.bashrc
    

Add the following line AFTER the list of other commands.

export HISTFILESIZE=200
    

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

source .bashrc
    

Check this has worked by typing

echo $HISTFILESIZE
      

Setting the path

When you type a command, your 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=~/units174/bin:$PATH
      

Not that here we append the existing path the new definition so we can still find commands like ls.

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.

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

export PATH=~/units174/bin:$PATH
    

Attribution

Original version by M.Stonebank@surrey.ac.uk, 9th October 2000. Modified by a.turner@lboro.ac.uk, 2015.