1

I am using module files to change the environment according to needs of my programs. exporting variables (setenv), appending PATH or LD_LIBRARY_PATH all works fine. But how do I make bash-completion work, preferably in a way that unloading the modulefile deactivates the bash completion?

Currently at the end of my modulefile I source a bash-script:

  #%Module1.0
  set BASE $env(opt)/gromacs/installation
  set GMXPREFIX $BASE
  setenv GMXBIN $GMXPREFIX/bin
  ...
  puts "source /opt/modulefiles/gromacs/.bash_completion

In .bash_completion I then try to access the set environment variable $GMXBIN:

  echo "creating bash completion for $GMXBIN."
  source $GMXBIN/gmx-completion.bash

gmx-completion.bash is itself a rather complex script (part of the installed software) using compgen and COMREPLY etc.

The result however is: when executing .bash_completion $GMXBIN is not set. If I execute echo $GMXBIN after loading the modulefile however it is. If I load the same modulefile a secondtime it works! So I have the feeling the sourcing of the .bash_completion script is done before the environment variables are set.

Something else: instead of using puts "source .bash_completion", I use system "source .bash_completion", it turns out source is not known while the module file is loaded.

2
  • I cant remember why, but did you try the eval command?
    – WGRM
    Mar 23, 2020 at 12:15
  • @WGRM To no avail.
    – JTT
    Mar 24, 2020 at 11:54

1 Answer 1

1

On older version of Modules (<4) code generated through the puts command is outputted first. On newer version of Modules (4+) it is outputted after all environment change.

Anyway, you can simplify your setup whatever the Modules version used as you can rely in the modulefile on the GMXBIN environment variable defined with the setenv command. You can access this variable value with $env(GMXBIN), thus you can directly source the $GMXBIN/gmx-completion.bash shell script.

So the /opt/modulefiles/gromacs/.bash_completion file can be dropped if modulefile's content is:

#%Module1.0
set BASE $env(opt)/gromacs/installation
set GMXPREFIX $BASE
setenv GMXBIN $GMXPREFIX/bin
...
puts "$env(GMXBIN)/gmx-completion.bash"

Going further, conditional switch may be used in modulefile to output different shell code whether the modulefile is loading or unloading. With that it is possible to source completion script on load and undo completed command (with complete -r) on unload:

#%Module1.0
set BASE $env(opt)/gromacs/installation
set GMXPREFIX $BASE
setenv GMXBIN $GMXPREFIX/bin
...
switch -- [module-info mode] {
    load {
        puts "source $env(GMXBIN)/.bash_completion"
    }
    unload {
        puts "complete -r <COMMAND>"
    }
}

Change <COMMAND> by the name of the completion to undo.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .