Setting environment variables from within TCL script

Attiq picture Attiq · Apr 17, 2015 · Viewed 19.3k times · Source

I was creating a Tcl script which will allow me to automate the installation of software. But the problem I am running into is that the software needs some environment variables set beforehand and I was wondering if its possible to set environment variables inside of the tcl script.

I was going to try exec /bin/sh -c "source /path/to/.bash_profile but that would create a shell script and source the variables into there and the tcl script wont pick it up then.

Can anyone give any other ideas?

Answer

glenn jackman picture glenn jackman · Apr 17, 2015

In Tcl you have the global env array:

set ::env(foo) bar

And then any child process has the variable foo in its environment.

If you want to put environment variables in a central file (i.e. .bash_profile) so that other programs can source them, then it should be pretty easy to get Tcl to parse that file and set the variables in the env array.