Version 3 of env

Updated 2003-02-06 16:12:57

The env array is one of the magic names created by Tcl to reflect the value of the invoker's environment; that is to say, when one starts Tcl, what one does is make a request to the operating system to start a new process. If the process is being started directly, that process is typically given a set of variables called environment variables which reflect certain default values. When Tcl starts, it creates the env array and reads the environment. Each environment variable becomes a key in this array whose value is the value of the environment variable.

For instance, on Unix, one will typically find keys like

  • PATH : series of file system directory prefixes, typically seperated by :, which the operating system will apply when asked to execute a file with an incomplete pathname.
  • HOME : name of the user's login directory.
  • LANG : string used to specify internationalization info.

To access the env array within a Tcl proc, one needs to tell the proc that env is a global array. There are two ways to do this.

  • global env ; puts $env(PATH) can be used to identify that the variable is globally available.
  • $::env(PATH) can alternatively be used.

The variables that appear in $::env are only those variables whom the parental shell places into the environment. Under bash/ksh/sh this is done by a line such as:

 MYVAR="this is a test" ; export MYVAR

while in csh/tcsh, you do it like this:

  setenv MYVAR "this is a test"

Shell variables set but not put into the environment are not accessible by child processes.

Also note that while a Tcl program can set new variables or values in $::env , those values will NOT be reflected in the program's parent process.

This separation of process data is an intentional part of the various operating systems available today. To communicate new variables or variable values back into the parent, one needs some sort of out of band communication of information; perhaps writing a shell script or file of data which the parent shell then reads.

[Add some category info here]