1

I'm having difficulties trying to use environment variables in TCL.

So I have a shell script which exports a bunch of environment variables so that my TCL scripts can use them later on.

set_vars.sh has the following code in it:

export TEST_ENV_VAR=Test

and the following works:

% puts $env(TEST_ENV_VAR)

Test

BUT once I place the environment variable in a operator or in quotes, nothing is returned.

% puts "$env(TEST_ENV_VAR) is in TEST_ENV_VAR"

is in TEST_ENV_VAR

Anyone know what the issue could be? Any help would be greatly appreciated.

Thanks for your time.

1
  • It seems the second time around, you did not run your set_vars.sh script. I tried your examples and they work just fine. Commented Mar 12, 2013 at 22:03

1 Answer 1

4

There must be something you're not showing us:

$ export TEST_ENV_VAR=Test
$ tclsh
% puts $env(TEST_ENV_VAR)
Test
% puts "$env(TEST_ENV_VAR) is in TEST_ENV_VAR"
Test is in TEST_ENV_VAR

Note that env is a global variable, so if you're using it in a procedure, you must declare it as global

$ tclsh
% proc show_env1 {varname} {
    puts "does not work: $env($varname)"
}
% show_env1 TEST_ENV_VAR
can't read "env(TEST_ENV_VAR)": no such variable
% proc show_env2 {varname} {
    global env
    puts "works: $env($varname)"
}
% show_env2 TEST_ENV_VAR
works: Test
% proc show_env3 {varname} {
    puts "also works: $::env($varname)"
}
% show_env3 TEST_ENV_VAR
also works: Test
Sign up to request clarification or add additional context in comments.

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.