Skip to content
Advertisement

Adapting “module” alias (invoking modulecmd) from tcsh to bash

In tcsh, I can run commands like:

 module add jdk/1.8.0_162 

…using an alias defined as such:

alias module 'eval `/app/modules/0/bin/modulecmd tcsh !*`'

I’m trying to get an equivalent to this for bash.


Currently, I’ve tried making a separate bash function for each subcommand, like so:

function module_add {
    /app/modules/0/bin/modulecmd bash add $1 2>>err.log
}

function module_rm {
    /app/modules/0/bin/modulecmd bash rm $1 2>>err.log
}

function module_list {
    /app/modules/0/bin/modulecmd bash list
}

java -version
module_list
module_rm 'j2re'
module_add 'jdk/1.8.0_162'
module_add 'gtk+/2.24.17'
module_list
java -version

I can be sure the program calls have been executed, since the unexisting module (added by purpose for testing) gtk+/2.24.17 creates an entry in err.log.

However, java -version still shows the same older version and module_list does not show any new modules. Everything works great in tcsh (where I use the module add alias instead). I have tried different versions of module command. The latest version tested is 3.2.10. The result is the same on RHEL6 and RHEL7

Any ideas how to solve this?

EDIT

Based on some clever comments I tried to the exact same command for tcsh

/app/modules/0/bin/modulecmd tcsh add jdk/1.8.0_162

and it gave the same result. Anyone who knows the difference between that command and

module add jdk/1.8.0_162 

in tcsh?

So I guess the question is rather about how the modulescmd differ from the tcsh alias module add

BR Patrik

Advertisement

Answer

modulecmd emits shell scripts on its stdout (thus the argument telling it which shell to generate a script for).

A shell needs to actually execute those commands for them to take effect; this is what eval does. (Don’t ever use eval unless you trust the people who wrote the program that generated the output you’re evaling to be rigorous about corner cases — it lends itself to security bugs).

Thus, if your existing tcsh alias for the module command is:

alias module 'eval `/app/modules/0/bin/modulecmd tcsh !*`'

…the bash equivalent would be:

module() { eval "$(/app/modules/0/bin/modulecmd bash "$@")"; }
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement