1

I'm having trouble using any predefined environmental variable in my module files. For example a line like this setenv TEST ${HOSTNAME} throws an error, where ${HOSTNAME} is defined by the system as a global environmental variable. The error looks like: ERROR:102: Tcl command execution failed: set TEST ${HOSTNAME}. I have set environmental variables in the module file itself and tried using those and get the same kind of error. For example,

This does not work:

setenv DUMMY /location/to/some/file setenv TEST ${DUMMY}

I get a similar error as above: ERROR:102: Tcl command execution failed: set TEST ${DUMMY}. However,

This works:

set DUMMY /location/to/some/file setenv TEST ${DUMMY}

There are certain lines that I needed to use predefined global environmental variables, so the above command cannot be used.

How can one use a predefined environmental variable in module files?

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
ExoticDFT
  • 13
  • 5

1 Answers1

2

The set command sets a Tcl variable. These are local to the module processing: they are not exported anywhere else. There are very few restrictions on what characters you can use in a Tcl variable name; about the only ones to really beware of are : and (, though there are a few others that can make things really awkward so it's still best to stick to alphanumerics (and _).

The setenv command (which is not a standard Tcl command, but rather something the module system adds) sets environment variables, and these are exported to subprocesses. They are also mapped as Tcl variables as elements of the global associative array, env, so you can do this

setenv DUMMY /location/to/some/file
setenv TEST $env(DUMMY)

(The restriction on ( that I mentioned up above is because this gets tangled up with the syntax for associative arrays. Element names can use any character at all. Environment variables probably ought to avoid ASCII NUL and = in their names…)


The implementation of setenv is probably something like this:

proc setenv {variable value} {
    global env
    set env($variable) $value
}

The mapping to env is bi-directional.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Thank you for your answer @DonalFellows. This seems to be working well for the `setenv` lines. So this solves that issue. However, I also have a few set-alias lines that are now complaining when using environmental variables in the alias. For example, `set-alias pbin 'cd $env(TEST)'` throws the same error as before. Do you have any insight on using these variables in aliases? Thanks! – ExoticDFT Jan 06 '16 at 15:57