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?
Show activity on this post. 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.
Environment variables are available to Tcl scripts in a global associative array env . The index into env is the name of the environment variable. The command puts "$env(PATH)" would print the contents of the PATH environment variable.
In side tcl script, you can simply do setenv as, setenv AUTOTEST="/auto/isbutest/frt" . if you want to set a variable, use set VARNAME "/auto/isbutest/frt" . if you want to get any environment variable, use $::env(AUTOTEST) .
namespace tail stringReturns the simple name at the end of a qualified string. Qualifiers are namespace names separated by double colons (::). For the string ::foo::bar::x, this command returns x, and for :: it returns an empty string. This command is the complement of the namespace qualifiers command.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With