How to read a shell environment variable in your Tcl script. So anyone please help me. I am very new in TCL.
4 Answers
Use $::env
to access any environment variables e.g. to access the TMP environment variable do this:
set tmpdir $::env(TMP)
More info here http://wiki.tcl.tk/1624

- 370,779
- 53
- 539
- 685

- 4,853
- 5
- 29
- 41
-
Just i write your code and execute it. But i am not getting. I am in tclsh prompt. My prompt is %. so what i did just type in the prompt % set tmpdir $::env(TMP). The output is like that can't read "::env(TMP)": no such variable – galvin Verghese Apr 01 '11 at 08:58
-
Actually i want to write a proc is tcl that returns the Bash environment variable values. – galvin Verghese Apr 01 '11 at 09:39
-
1That's because you have no environment variable called TMP. Did you read the link I gave you? – TrojanName Apr 01 '11 at 11:17
-
Ah, I can see the conversation you're having with the other responder - I think you'll be ok! – TrojanName Apr 01 '11 at 11:20
$ export var=42
$ tclsh
% puts $env(var)
42

- 3,647
- 1
- 18
- 26
-
TMP environment variable is define in the bash script. so what i want to do just access the environment variable in the tcl. my tcl prompt is %. so finally i want to access the environment variable in tcl. – galvin Verghese Apr 01 '11 at 09:03
-
The TMP enviroment variable would be $env(TMP). **But**: Is the tcl script called from the bash script? Is the variable global (ie. export)? – hynek Apr 01 '11 at 09:06
-
Yea. It is working fine. but my question is that what ever environment variable is defined in bash or shell script just i want to access using the proc in tcl. so please........ – galvin Verghese Apr 01 '11 at 09:08
-
yea. TMP variable is the environment variable that is defined in bash script. TMP=/tmp ORACLE_SID=DBTEST ORACLE_HOME=/opt/oracle/product/102 export ORACLE_SID ORACLE_HOME so here i want to access the environment variable ORACLE_SID and ORACLE_HOME, so how i should do, please ........ – galvin Verghese Apr 01 '11 at 09:13
-
-
-
It's all analogous: The bash variable ORACLE_SID can be accessed using $env(ORACLE_SID) in your tcl script. – hynek Apr 01 '11 at 09:20
-
Please just edit your question with the steps, that will be the easiest. – hynek Apr 01 '11 at 09:21
-
See what i am doing here. I am writing my all steps: 1. I am using linux 5.0 server. so i am in admin prompt and my prompt is #. My Prompt is [root@db1~] #. 2. after that what i did type tclsh. 3. so now i am in tcl prompt i.e. % (percentage) 4. Type here: su - oracle 5. after that my current working directory is /home/oracle/ and my prompt is $(Dollar). 6. now i again type here tclsh 7. so now i am in % prompt and after that final step 8. puts $env(ORACLE_SID) 7. so here i am getting the exact output DBTEST. so there are the process i am doing here. But what i want i want to write a tcl proc – galvin Verghese Apr 01 '11 at 09:29
-
so finally i want to write a tcl proc to return the bash environment variable. please help me – galvin Verghese Apr 01 '11 at 09:31
-
Your problem seems to be the su rather than reading out the variable. You can't just call "su - oracle" inside of a script and read out the bash variables. The easiest way I can suggest is running your tcl script as 'su - oracle -c "tclsh /absolute/path/your-script.tcl"' - you can write a bash wrapper around that of course. – hynek Apr 01 '11 at 09:43
Environment variables are accessible via the built-in global variable env
(fully qualified it is ::env
). You use this like any other Tcl array.
If you want to print a list of all environment variables you can use something like this:
proc dump_env_vars {} {
foreach name [array names ::env] {
puts "$name == $::env($name)"
}
}
Of course, to access just a single variable you use it like any other array, for example:
puts "HOME = '$::env(HOME)'"
For more information see the env page on the Tcler's wiki and the env section of the tclvars man page

- 370,779
- 53
- 539
- 685
-
yea, your procedure is executed, it gives all the environment variables. its ok. But what i am asking here. Suppose in bash script if i define the user define environment like this : ORACLE_SID=DBTEST ORACLE_HOME=/opt/oracle/product/102 export ORACLE_SID ORACLE_HOME Now i want to write a proc in tcl to access the environment variable that is defined by user. so how can i do it. please.......... – galvin Verghese Apr 02 '11 at 05:29
-
I also read u r link. But it is not match with my requirement. – galvin Verghese Apr 02 '11 at 05:49
-
I just use `parray env` when I want to dump environment variables. – Donal Fellows Apr 02 '11 at 07:34
-
Here Actually parray is used for print the env values. But what i want how to access the environment variable in TCL, that is defined in bash script, so please help me...... – galvin Verghese Apr 06 '11 at 06:09
To read a shell environment variable in Tcl script, try doing something like this:
global env
set olddisplay $env(DISPLAY)
set env(DISPLAY) unix:0
This might be expressed as well in this fashion:
set olddisplay $::env(DISPLAY)
set ::env(DISPLAY) unix:0
and forget about global
.
You can check to see if the variable exists by doing something like:
if {[info exists env(VARNAME)]} {
# okay, it's there, use it
set value $env(VARNAME)
} else {
# the environment var isn't set, use a default
set value "the default value"
}
This is source.

- 4,611
- 10
- 37
- 53