Search code examples
bashenvironment-variablesmodulefile

Using bash variables inside of modulefiles


I'm trying to write a series of modulefiles that can be used to load software installed under each user's account in a cluster setting. Since the software is installed per person under similar paths up-to a username changing, I'm trying to dynamically set the base path value in the modulefile.

For instance, if I could use an absolute path, then I would set a BASEPATH like so:

#%Module1.0#####################################################################
##
## libevent 2.1.11

set     app libevent
set     version 2.1.11
module-whatis   "loads the necessary `$app-$version' library paths"

set BASEPATH /usr/local/packages/dev/$app/$version

prepend-path    LD_LIBRARY_PATH     $BASEPATH/lib
prepend-path    LD_RUN_PATH             $BASEPATH/lib
prepend-path --delim " " LDFLAGS "-L$BASEPATH/lib"
prepend-path --delim " " CPPFLAGS "-I$BASEPATH/include"
prepend-path --delim " " CFLAGS "-I$BASEPATH/include"

However, in my case, I need the BASEPATH variable to be:

set BASEPATH /home/${USER}/software/$app/$version

where $USER would expand to the current username.

Unfortunately, when loading the module, I receive:

libevent/2.1.11(15):ERROR:102: Tcl command execution failed: 
set BASEPATH /home/${USER}/software/$app/$version

Solution

  • Turns out that bash variables can be accessed in modulefiles by using TCL language's env array given by $::env().

    So, in my case, I would use:

    set BASEPATH /home/$::env(USER)/software/$app/$version
    

    or, equivalently,

    set BASEPATH $::env(HOME)/software/$app/$version