0

I have a tcl script that needs to checks the version and license (among other things) of a tool that is used within the script.

I can check to see what the environment variables are by simply:

puts $::env(VERSION_PATH) 

And it would return the same value that would result from:

# VV Ran outside the script VV
printenv | grep VERSION_PATH

But if the compare portion of my code fails, I would like to give VERSION_PATH the correct value.

So I have been trying a variety of different commands to achieve this:

set env(VERSION_PATH)   /tool/bin/misc_path    
set ::env(VERSION_PATH) /tool/bin/misc_path
puts "export VERSION_PATH=/tool/bin/misc_path"

1st/2nd line source, under LH

3rd line source

I am fairly new to linux/shells so if this is something that just can't be done, I'd just like to know :)

Any advice/suggestions would be greatly appreciated.

Thank you

4
  • The environment variables are copied from the OS into the env array. If you change any values in the env array, the real environment variables won't be affected. When the process ends, your changes are lost. If your process starts child processes, they will get a copy of your changed environment, but that's as far as you can get. Commented Sep 26, 2014 at 21:15
  • As mentioned by others, this is not possible in any programming language. Not even bash (the workaround for bash is to source the script instead of executing it, but since bash doesn't understand tcl or perl or ruby or C or pascal or anything that is not bash you can only write such scripts in bash) Commented Sep 27, 2014 at 15:36
  • As for why this is not possible, it's the same reason web browsers have same-origin-policy. The original designers of unix decided that letting child processes mess with the parent's environment is a security risk. Commented Sep 27, 2014 at 15:37
  • On Windows you can set a global environment variable and notify other processes of that change, but thats quite a different beast and requires cooperation by handling WM_SETTINGCHANGED, so its probably not what you are looking for (see msdn.microsoft.com/en-us/library/windows/desktop/… if you need to know more). Commented Sep 27, 2014 at 15:43

2 Answers 2

3

You cannot set the environment variables of a parent process, by design. This isn't a tcl limitation per se, it's just how shells and environment variables work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the response, really appreciate it :)
3

Environment variables are inherited by being copied from the parent process to each subprocess that it creates. The child process cannot touch the parent's copy.

This doesn't mean that it can't be done; it's just not something that can happen quite automatically. The easiest way to set a parent's environment variable, provided the parent process is expecting this, is to write a short script to the child process's stdout and get the parent to evaluate whatever the child process returns.

Thus, with this Tcl script:

# Compute this value any way you want
set versionPath /tool/bin/misc_path
puts "export VERSION_PATH=$versionPath"

You can then have this Bash script call it like this:

eval `tclsh getVersionPath.tcl`

After that, the bash code will have the environment variable. (This is the core of how the modules system works on some Unixes, even if it isn't universally installed.) In short, as long as the outer process is expecting to run a subprocess that gives it a script to evaluate which will set up the environment, it can work.

(You can do the same on Windows too, but that uses a somewhat different syntax and pretty much requires that you write the script to a temporary file and use CALL theTempFile.bat.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.