Using Environment Variables

Using Environment Variables

This page is assuming you use bash or another shell in the Bourne family of shells. Most of the informtation will be correct for other shells as well, but the examples cannot be assumed to be.

HPC2N recommends using bash, since it is the only shell that is fully supported by the batch system.

Environment variables are a set of dynamic named values, generally meant to make your life easier. Some are built-in, some are set by loading various modules, and you can also define extra to help yourself.

An example could be an environment variable with a standard name that stores the location that a particular computer system uses to store temporary files.

You can see your current environment variables by invoking the command 'env' or list a specific variable with 'echo $ENVIRONMENT_VARIABLE'. It is important to remember the dollar sign.

Example: listing all your environment variables

$ env
MODULE_VERSION_STACK=3.2.7
TERM=xterm
SHELL=/bin/bash
RANDFILE=/dev/urandom
SSH_TTY=/dev/pts/22
COLUMNS=80
EDITOR=/usr/bin/vim
LANG=en_US.UTF-8
...
$

The above is shortened, as it tends to be long.

In shells, like bash (which is the recommended shell at HPC2N), you can change the environment variables by using 

export ENVIRONMENT_VARIABLE=VALUE <command>

If you do not use 'export' when setting or creating a variable, it will only have meaning in the current shell.

Some common environment variables and their meaning:

  • HOME contains the user's home directory
  • PATH lists the directories the shell searches for the commands the user can type without having to provide the full path
  • HOSTNAME contains the name of the host you are currently connected to
  • PWD (print working directory) is used to output the path of the current working directory.
  • KRB5CCNAME points at a file that contains your Kerberos credentials
  • SHELL your current shell (bash, tcsh, csh, ksh)
  • PBS_O_WORKDIR contains the directory you submitted your job script from

A list of variables relevant for SNIC, as well as a short description of these, can be found here.

Example, creating your own shell variables:
Assume you have the binary of your favourite program in the directory projects/coolness/bin in your home directory. You would like a variable for this directory, both to be able to change to it quickly, and to be able to easily set the path to it when you want to use it. Notice that you are using the environment variable $HOME for your home directory here.

First issue the command

VARIABLE_NAME=$HOME/projects/coolness/bin

and then, regardless of what directory you are in, you can issue

cd VARIABLE_NAME

to go quickly to the directory containing your cool program.

A variable assignment like this will work just fine, but its scope (visibility) is limited to the current shell. If you launch a program or enter another shell, that child task will not know about your environment variables unless you export them first.

Unless you know for sure that an environment variable will have meaning only in the current shell, it's a good idea to always use export when creating variables to ensure they will be global in scope.

Example

export VARIABLE_NAME=$HOME/projects/coolness/bin

To not have to retype them every time you need them (after logging in again), you can add them to your .profile or .bashrc (at HPC2N you should make the changes to the file Public/.bashrc). 

Example, adding a directory to your path
Say you have written and compiled a new program and wants to be able to run it without typing out the whole path every time. You can achieve this by adding the directory to your path.

See your path with the command echo $PATH.

Example:

$ echo $PATH
/home/u/user123/Public/bin:/usr/local/bin:/usr/heimdal/bin: \
/usr/bin/X11:/usr/bin:/usr/bsd:/usr/gnu/bin:/bin/:/usr/local/sbin: \
/sbin:/usr/sbin:/usr/etc:/usr/heimdal/sbin:/usr/local/sbin
$

(\ is used to signify that the line was too long and has been broken in two)
The default path is set in /afs/hpc2n.umu.se/public/env/shell/bashrc, but you make changes/additions to a custom .bashrc that is located in your Public directory.

To add your directory /home/u/user123/coolness to the path, open the file Public/.bashrc with your favourite editor and add a colon and then the directory after what is already in the (customized) path. If no customizations have been done, and there is no entry for the path, add PATH=$PATH and then the colon and the directory. Like this:

PATH=$PATH:/home/u/user123/coolness
Updated: 2017-09-21, 11:05