BYU

Office of Research Computing

Chapter 8: Environment variables

8.1 About environment variables and how display them

Environment 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. By convention, environment variables have UPPER CASE names.

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

  • USER (your login name)
  • HOME (the path name of your home directory)
  • HOST (the name of the computer you are using)
  • ARCH (the architecture of the computers processor)
  • DISPLAY (the name of the computer screen to display X windows)
  • PRINTER (the default printer to send print jobs)
  • PATH (the directories the shell should search to find a command)

On the supercomputer, some of the most useful environment variables are very specific to particular programs. Many of the commercial applications and programs that we provide allow you to set environment variables prior to running them, which will affect the way that they function and run. In some cases, setting the right environment variable can eliminate errors or problems that occur when running these commercial applications.

Finding out the current values of these variables.

Environment variables can be displayed using the env or printenv commands. To show all values of these variables, type

$ env | less

8.2 Using and setting variables

At the command line, environment variables can be set using the export command. For example,

$ export SOMEVARIABLE=somevalue

Would set the SOMEVARIABLE to "somevalue".

Variables can also be unset using the unset command. This might be helpful if you accidentally defined a variable that you didn't want defined. For example,

$ unset SOMEVARIABLE

Would clear whatever value was stored in the variable SOMEVARIABLE.

8.3 Setting environment variables in the .bashrc file

In many cases you will want a particular environment variable or set of environment variables to be defined each time you log in to the supercomputer. This process can be automated using the .bashrc file in your home directory.

The default bash shell looks in .bashrc for user defined commands to run at each login. (Note that the file name begins with a dot). It is here that you can add commands to define variables and perform various other tasks that need to be automated each time you log in.

WARNING: NEVER run graphical programs (e.g. a web browser) from your .bashrc file.

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

$ nano ~/.bashrc

Add the following line at the end of the file:

export HISTSIZE=2000 The variable name, the "=", and the variable value must not be separated by space.

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

$ source .bashrc

Check this has worked by typing

$ echo $HISTSIZE

8.4 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 (~/units187/bin/units), or you need to have the directory ~/units187/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:~/units187/bin"

Test that this worked by trying to run units in any directory other than 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 at the end of the file:

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

Summary

env List environment variables and their values
export SOMEVARIABLE=somevalue Set the variable SOMEVARIABLE to the value somevalue
unset SOMEVARIABLE Unset the variable SOMEVARIABLE
source filename Import the contents of filename into the current environment