Skip to content

Using Environment Variables

Environment variables allow you to customize the environment in which programs run. For example, you can define an environment variable to set the path to data files, or enable or disable features.

SuperCloud has created some modulefiles that will set environment variables that allow you to select a particular version of executables or libraries, specify the location of datasets, or enable/disable functionality. In order use these modulefiles, you'll have to load them from the Linux command prompt.

On this page, we will go over how to use environment variables in:

  • Linux shell script
  • Slurm environment
  • Python
  • MATLAB®

In the instructions below, VARNAME is the name of the environment variable, and value is the value assigned to the environment variable. By convention, environment variable names are all UPPERCASE.

Using environment variables in a Linux shell script

bash shell variable naming

The Linux bash shell is pretty particular with its variable names. A name is a word consisting only of alphanumeric characters and underscores, and beginning with an alphabetic character or an underscore. In addition, there are a few naming conventions:

  • Variable Names: Lower-case, with underscores to separate words. Example: my_variable_name
  • Constants and Environment Variable Names: All caps, separated with underscores, declared at the top of the file. Example: MY_CONSTANT

To get the value of an environment variable

In a Linux shell script, you get the value of an environment variable by adding the dollar sign $ in front of the environment variable name.

$VARNAME

Example of saving the value of an environment variable to a variable local to your script:

path_to_data=$IMAGENET_PATH

Notice that there are no spaces on either side of the equals sign. In Linux, there must not be any spaces around the equals sign when assigning the value of an environment variable to another variable.

To set the value of an environment variable

You can set environment variables from a Linux shell script that remain in effect while your script and any child processes that it creates are running. Environment variables are local to the process in which they were set. When a child process is created, it inherits all the environment variables and their values from the parent process.

VARNAME=value

Example of setting the value of an environment variable:

IMAGENET_PATH=/home/gridsan/groups/datasets/ImageNet

Notice that there are no spaces on either side of the equals sign. In Linux, there must not be any spaces around the equals sign when assigning a value to an environment variable.

Using Slurm environment variables

Once a job is submitted to Slurm, Slurm provides information about its execution environment through environment variables. For example: the working directory, the job id, total number of tasks in a job array, job array id number. These Slurm environment variables may be useful in your code and for monitoring your jobs.

You can find a full list of Slurm's environment variables on these pages:

To get the value of an environment variable

To get the value of a Slurm environment variable, add a dollar sign $ in front of the environment variable name.

$VARNAME

Example of passing the array task id and count as parameters to your python code:

python test.py $SLURM_ARRAY_TASK_ID $SLURM_ARRAY_TASK_COUNT

Setting the value of an environment variable

The Slurm input environment variables are assigned values based on the flags that are passed to Slurm when the job is submitted.

Using environment variables in Python

To get the value of an environment variable

import os os.environ.get('VARNAME') os.getenv('VARNAME')

To set the value of an environment variable

In Python, when you set an environment variable, it is set for the Python session.

import os os.environ['VARNAME'] = 'value'

It's important to remember that the settings you apply in a Python script don't work outside that specific process; os.environ doesn't overwrite the environment variables system-wide.

Using environment variables in MATLAB®

To get the value of an environment variable

getenv('VARNAME')

To set the value of an environment variable

setenv('VARNAME','value')