Environment variables¶
Environment variables store data that is used by the operating system and other programs.
Some are intrinsic to the operating system, some for a specific program/library/programming language, and some are created by the user.
The variables can both be used in scripts and on the command line. Usually you reference them by putting a special symbol in front of or around the variable name. By convention, environment variable names are in UPPER CASE.
Examples:
- $HOME Your home directory
- $PWD This variable points to your current directory
- $LD_LIBRARY_PATH a colon-separated list of directories that the dynamic linker should search for shared objects before searching in any other directories
- $OMP_NUM_THREADS Number of OpenMP threads
- $PYTHONPATH Path to the directory where your Python libraries and packages are installed
Tip
You will get a long list of all environment variables currently set with the command:
You could also use
In their default state they work the same. The difference is twofold.
- You can use
printenv
to request the value of individual variables: - You can use
env
to modify the environment that programs run in by passing a set of variable definitions into a command like this:
Some environment variables need to be exported in order to be used
This is how you set the environment variable VARIABLE to value:
For the bash
(and related) shells:
For csh
and related shells:
You can create your own variables to use, for instance in scripts.
Creating your own variable
I create a variable called MINE and set it to /usr/bin/gcc
Check it is set:
You can now (until you start a new session) use $MINE instead of gcc:
Examples¶
!! note “More useful example of creating your own variable”
Assume you have a script you run in a different directory than where you have the datafiles. You could then use an environment variable to give the path to the data directory instead of each time writing the full path:
```bash
$ export DATAPATH=/home/bbrydsoe/project/dataset1/
```
Then you can refer to that directory with $DATAPATH in your script.
Warning
The environment variable set this way only retains the value you have set for the duration of the session. When you open a new terminal window or login again, you need to set it again.
To avoid that, add the environment variable to your .bashrc file, but only do so if it should truly be persistent across many sessions (like adding a new directory to search to LD_LIBRARY_PATH
for instance).
Quickly add a new directory to LD_LIBRARY_PATH in your .bashrc
Change /your/custom/path/
to the actual path to the directory for your library.
Exercise¶
- Run
env
to see the environment variables that are set at your computer - Use
echo
to see the content of$PWD
- try change to another directory and run it again. - Create your own variable. Set it to something (export) and use
echo
to see that it has the right value. - Use
printenv
to tell you the value of an environment variable.
Summary¶
Keypoints
- environment variables are used to store data that is used by the operating system and other programs
- Some common environment variables are:
- $HOME Your home directory
- $PWD This variable points to your current directory
- $LD_LIBRARY_PATH a colon-separated list of directories that the dynamic linker should search for shared objects before searching in any other directories
- $OMP_NUM_THREADS Number of OpenMP threads
- $PYTHONPATH Path to the directory where your Python libraries and packages are installed
- You can create own environment variables
env
is usefulprintenv
is useful