Unix shells read a variety of config files (/etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile, and ~/.bashrc) at various times depending on how they are invoked, whether they are login shells or interactive shells or both, etc. This stuff can be confusing to the beginner. This document explains how to set things up in a hopefully straightforward way.
The correct shell environment corresponding to a workspace is generated by cmake and written to $I3_BUILD/env-shell.sh. This script contains the correct settings for PATH, LD_LIBRARY_PATH, PYTHONPATH and so forth. The correct settings depend on which tools are available in your toolset, which metaproject and version you are running, etc.
Executing this script at the command line will spawn a subshell with the appropriate environment:
% ./env-shell.sh
************************************************************************
* *
* W E L C O M E to I C E T R A Y *
* *
* Version offline-software.trunk r49100 *
* *
* You are welcome to visit our Web site *
* http://icecube.umd.edu *
* *
************************************************************************
Icetray environment has:
I3_SRC = /home/troy/Icecube/meta-projects/offline-software/trunk/src
I3_BUILD = /home/troy/Icecube/meta-projects/offline-software/trunk/build
I3_PORTS = /opt/i3/ports
Under the hood there are many other environment variables set. You can look at the file to see exactly what. To unload this environment, simply exit the subshell:
% exit
Exited Icetray Environment.
%
Because this script executes a subshell and is responsible for setting certain environment variables, misconfigured shell initialization files (.bashrc and the like) can spoil the party by overwriting the intended settings. For example, the env-shell.sh should prepend the environment variable PATH with $I3_BUILD/bin. The correct behavior would look something like this:
% echo $PATH
/usr/bin:/bin
% ./env-shell.sh
************************************************************************
* *
* W E L C O M E to I C E T R A Y *
* *
* Version offline-software.trunk r49100 *
* *
* You are welcome to visit our Web site *
* http://icecube.umd.edu *
* *
************************************************************************
Icetray environment has:
I3_SRC = /home/troy/Icecube/meta-projects/offline-software/trunk/src
I3_BUILD = /home/troy/Icecube/meta-projects/offline-software/trunk/build
I3_PORTS = /opt/i3/ports
% echo $PATH
/home/troy/Icecube/meta-projects/offline-software/trunk/build/bin:/opt/i3/ports/root-v5.18.00/bin:/opt/i3/ports/bin:/usr/bin:/bin
You can see that also the path to the ROOT installation in the toolset has been added. If I erroneously assign to PATH in my .bashrc, ie if .bashrc contains the following line:
PATH=/path/to/my/stuff
then the desired settings will be overwritten by my subshell as it initializes:
% echo $PATH
/usr/bin:/bin
% ./env-shell.sh
************************************************************************
* *
* W E L C O M E to I C E T R A Y *
* *
* Version offline-software.trunk r49100 *
* *
* You are welcome to visit our Web site *
* http://icecube.umd.edu *
* *
************************************************************************
Icetray environment has:
I3_SRC = /home/troy/Icecube/meta-projects/offline-software/trunk/src
I3_BUILD = /home/troy/Icecube/meta-projects/offline-software/trunk/build
I3_PORTS = /opt/i3/ports
% echo $PATH
/usr/bin:/bin
Assume you have a ROOT installation in a special location; in order to use it you need to set ROOTSYS, LD_LIBRARY_PATH and PATH.
Warning
You don’t want to actually mix root versions like this. This is almost guaranteed to cause subtle errors that manifest themselves in bizarre ways. Root here just means “some tool that I’ve had to set up myself”.
As explained, if you simply modify these variables in your .bashrc, you’ll have problems later on. There are a few ways to handle this:
In the initialization files you could put the assignments to these variables inside an if-then block so that they are executed only once. Here it is .bashrc or .zshrc -style:
#
# if MY_ENV_SET_ALREADY has zero length,
#
if [ -z "$MY_ENV_SET_ALREADY" ]; then
#
# set my variables
#
export PATH=/path/to/my/root/bin:$PATH
export ROOTSYS=/path/to/my/root
#
# set the flag that we shouldn't do this in the future
#
export MY_ENV_SET_ALREADY=true
fi
So the block that sets PATH and ROOTSYS will execute only on the first spawn of a subshell.
You should be able to get the same effect with .tcsh, (though I personally wouldn’t try). Have a look at http://www.grymoire.com/Unix/CshTop10.txt and http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ .
If you have a complicated environment or lots of tools, you may want to put the settings into shell functions that you can run when you need to use particular tools (again, this in .bashrc):
use_root518() {
echo "Setting environment for root 5.18"
export ROOTSYS=/path/to/my/root-5.18.00
export PATH=$ROOTSYS/bin:$PATH
export LD_LIBRARY_PATH=$ROOTSYS/lib:$LD_LIBRARY_PATH
}
use_root520() {
echo "Setting environment for root 5.20"
export ROOTSYS=/path/to/my/root-5.20.00
export PATH=$ROOTSYS/bin:$PATH
export LD_LIBRARY_PATH=$ROOTSYS/lib:$LD_LIBRARY_PATH
}
Your default shell will have none of this special stuff loaded, but you can configure things easily:
$ echo $PATH
/usr/bin
$ use_root520
Setting environment for root 5.20
$ echo $PATH
/path/to/my/root-5.20.00/bin:/usr/bin
And these functions may even be run after the env-shell.sh has been run:
% echo $PATH
/usr/bin
% ./env-shell.sh
************************************************************************
* *
* W E L C O M E to I C E T R A Y *
* *
* Version offline-software.trunk r49100 *
* *
* You are welcome to visit our Web site *
* http://icecube.umd.edu *
* *
************************************************************************
Icetray environment has:
I3_SRC = /home/troy/Icecube/meta-projects/offline-software/trunk/src
I3_BUILD = /home/troy/Icecube/meta-projects/offline-software/trunk/build
I3_PORTS = /opt/i3/ports
% echo $PATH
/home/troy/Icecube/meta-projects/offline-software/trunk/build/bin:/opt/i3/ports/
root-v5.18.00/bin:/opt/i3/ports/bin:/usr/bin
% use_root518
Setting environment for root 5.18
% echo $PATH
/path/to/my/root-5.18.00/bin:/home/troy/Icecube/meta-projects/offline-software/t
runk/build/bin:/opt/i3/ports/root-v5.18.00/bin:/opt/i3/ports/bin:/usr/bin
Warning
Again: you don’t want to try to override the ROOT that a metaproject is built against like this. This is almost guaranteed to cause subtle errors that manifest themselves in bizarre ways. Root here just means “some tool that I’ve had to set up myself”.
Refer to your shell’s man page for further detail regarding shell invocation and initialisation (section “INVOCATION” for bash, section “STARTUP/SHUTDOWN FILES” for zsh).