Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Env variable change

97 views
Skip to first unread message

Chendu

unread,
Aug 20, 2007, 11:10:46 PM8/20/07
to
Hello
How to set the environment variable through tcl.

setenv <name> <value>

if i execute the same it not working.Is there any specific way in tcl
where in we can set the variable.

Thanks
chendu

Michael Schlenker

unread,
Aug 21, 2007, 2:14:12 AM8/21/07
to
Chendu schrieb:
set ::env(name) value

e.g.

set ::env(PATH) /usr/local/bin


Michael

Donal K. Fellows

unread,
Aug 21, 2007, 5:02:59 AM8/21/07
to
Michael Schlenker wrote:
> Chendu schrieb:

>> How to set the environment variable through tcl.
>> setenv <name> <value>
>> if i execute the same it not working.Is there any specific way in tcl
>> where in we can set the variable.
>
> set ::env(name) value

For those that like setenv...

proc setenv {name value} {
set ::env($name) $value
}

Donal.

Cameron Laird

unread,
Aug 21, 2007, 7:26:20 AM8/21/07
to
In article <fae9o7$594$1...@godfrey.mcc.ac.uk>,

Understand, this does NOT set the environment of the process which
*invokes* a Tcl application; the latter is often what questioners
think they want <URL: http://wiki.tcl.tk/4282 >.

Donal K. Fellows

unread,
Aug 21, 2007, 8:44:00 AM8/21/07
to
Cameron Laird wrote:
> Understand, this does NOT set the environment of the process which
> *invokes* a Tcl application; the latter is often what questioners
> think they want <URL: http://wiki.tcl.tk/4282 >.

Mind you, nothing does except editing a file/registry entry[*] and
restarting the shell or reloading its configuration. This is by
deliberate design, and applies to thing other than Tcl.

Donal.
[* Depending on platform. ]

Cameron Laird

unread,
Aug 21, 2007, 9:48:19 AM8/21/07
to
In article <faemmk$8r8$1...@godfrey.mcc.ac.uk>,

Donal K. Fellows <donal.k...@manchester.ac.uk> wrote:
.
.
.
F'sure[*], as the Wiki page above instructs, but the

source `my_example.tcl`
or
eval ... $EXECUTABLE ...

or similar trick mentioned in that same Wiki page often
comes as a revelation to those asking questions in this
area.

Alan Anderson

unread,
Aug 21, 2007, 7:31:00 PM8/21/07
to
"Donal K. Fellows" <donal.k...@manchester.ac.uk> wrote:

Sufficient knowledge of the internal data structures of the platform
lets you do theoretically impossible things. In my early career as a
software developer, I wrote a large collection of DOS programs that
communicated with each other by manipulating the "root" environment.

(There was much about that project I should have done differently, or
not done at all. Basing the entire architecture of the user interface
and network communication around the capabilities of a Hercules Network
Card Plus made for a fast and reasonably pretty system, but when the
supply of monochrome monitors started to dry up...)

Donal K. Fellows

unread,
Aug 21, 2007, 8:18:25 PM8/21/07
to
Alan Anderson wrote:
> Sufficient knowledge of the internal data structures of the platform
> lets you do theoretically impossible things. In my early career as a
> software developer, I wrote a large collection of DOS programs that
> communicated with each other by manipulating the "root" environment.

You can't do that on "modern" systems, i.e. anything with virtual memory
or real memory protection! You either can't see the root environment at
all, or can't recognize it, or can't write it (the most common case.)

Donal.

Gerald W. Lester

unread,
Aug 21, 2007, 9:43:39 PM8/21/07
to

You can, if you have enough privileges -- or at least the right ones.

After all that is how ACPs work.

--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+

Chendu

unread,
Aug 23, 2007, 2:24:48 AM8/23/07
to

i am able to set env variable and able to get it with in the same tcl
script .

But if i want to access in the value in other tcl script with out
editing the shell rc file.

Is there any way
Process:
1.I would open a shell.
2.run a tcl script to set a env variable
3.run a second tcl script to accss the env variable set by 1st
script.

i tried using as in wiki
source export myvar=<value>
but i am not able to get it. basically i am not able to get what
export does.

Thanks
chendu

Bryan Oakley

unread,
Aug 23, 2007, 6:58:46 AM8/23/07
to
Chendu wrote:
>
> i am able to set env variable and able to get it with in the same tcl
> script .
>
> But if i want to access in the value in other tcl script with out
> editing the shell rc file.
>
> Is there any way
> Process:
> 1.I would open a shell.
> 2.run a tcl script to set a env variable
> 3.run a second tcl script to accss the env variable set by 1st
> script.
>

No. That's not how environment variables work. This is not something
under Tcl's control. If a process sets environment variables, the only
other processes that can see those changes are processes spawned by the
one that changes the environment (assuming those variables are properly
exported)

> i tried using as in wiki
> source export myvar=<value>
> but i am not able to get it. basically i am not able to get what
> export does.

Export says "make this available to any child this process spawns", and
it's not a tcl command, it's a command in some shells.

You might be interested to read more about environment variables on the
wikipedia: http://en.wikipedia.org/wiki/Environment_variable

Gerald W. Lester

unread,
Aug 23, 2007, 8:21:10 AM8/23/07
to
Chendu wrote:
> ...

> But if i want to access in the value in other tcl script with out
> editing the shell rc file.
>
> Is there any way
> Process:
> 1.I would open a shell.
> 2.run a tcl script to set a env variable
> 3.run a second tcl script to accss the env variable set by 1st
> script.

No.

For steps 2 and 3 you could replace the string "tcl script" and "1st script"
with "a program I wrote" and be correct -- at least for anyone needing to
ask the question.

Glenn Jackman

unread,
Aug 23, 2007, 9:10:04 AM8/23/07
to
At 2007-08-23 02:24AM, "Chendu" wrote:
> But if i want to access in the value in other tcl script with out
> editing the shell rc file.

As others have said, the child process cannot write variables in the
parent process.

> Is there any way
> Process:
> 1.I would open a shell.
> 2.run a tcl script to set a env variable
> 3.run a second tcl script to accss the env variable set by 1st
> script.

What you do is have the child process suggest changes to the parent, but
the parent has to decide what to do with it:
1. open a shell
2. call the tcl "config" script: it prints to stdout like this:
export var1=val1
export var2=val2
Use whatever syntax your shell requires.
3. your shell captures and evaluates the output of #2.
4. ?
5. Profit! (with apologies to /.)

Like this:

$ cat var.tcl
#! /usr/local/bin/tclsh
puts "export FOO=bar"
puts "export BAZ=qux"

$ echo "FOO='$FOO', BAZ='$BAZ'"
FOO='', BAZ=''

$ eval `./var.tcl`

$ echo "FOO='$FOO', BAZ='$BAZ'"
FOO='bar', BAZ='qux'


--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Larry W. Virden

unread,
Aug 23, 2007, 10:07:50 AM8/23/07
to

What do you mean by "it [is] not working"?

Can you give us a specific example of what you are trying to
accomplish?

Larry W. Virden

unread,
Aug 23, 2007, 10:11:18 AM8/23/07
to
On Aug 23, 2:24 am, Chendu <pjsent...@gmail.com> wrote:
>
> Is there any way
> Process:
> 1.I would open a shell.
> 2.run a tcl script to set a env variable
> 3.run a second tcl script to accss the env variable set by 1st
> script.
>
> i tried using as in wiki
> source export myvar=<value>
> but i am not able to get it. basically i am not able to get what
> export does.

On what platform are you running? For instance, on Unix, something
like this would work.

$ MYVARIABLE=$(firsttclscript)
$ export MYVARIABLE
$ SecondTclScriptThatUsesMYVARIABLE

Cameron Laird

unread,
Aug 23, 2007, 10:21:34 AM8/23/07
to
In article <slrnfcr1pc...@smeagol.ncf.ca>,

Glenn Jackman <gle...@ncf.ca> wrote:
>At 2007-08-23 02:24AM, "Chendu" wrote:
.
.

.
>What you do is have the child process suggest changes to the parent, but
>the parent has to decide what to do with it:
> 1. open a shell
> 2. call the tcl "config" script: it prints to stdout like this:
> export var1=val1
> export var2=val2
> Use whatever syntax your shell requires.
> 3. your shell captures and evaluates the output of #2.
> 4. ?
> 5. Profit! (with apologies to /.)
>
>Like this:
>
>$ cat var.tcl
>#! /usr/local/bin/tclsh
>puts "export FOO=bar"
>puts "export BAZ=qux"
>
>$ echo "FOO='$FOO', BAZ='$BAZ'"
>FOO='', BAZ=''
>
>$ eval `./var.tcl`
>
>$ echo "FOO='$FOO', BAZ='$BAZ'"
>FOO='bar', BAZ='qux'
.
.
.
Chendu, is Unix your target? If I understand you correctly,
Glenn has entirely answered your question.

Darren New

unread,
Aug 23, 2007, 11:35:53 AM8/23/07
to
Chendu wrote:
> 1.I would open a shell.
> 2.run a tcl script to set a env variable
> 3.run a second tcl script to accss the env variable set by 1st
> script.

The question nobody has asked is why you feel the need to use
environment variables for this. Environment variables are a very limited
form of interprocess communication, and their restrictions are such that
it's very difficult to do the sort of thing you're trying to do.

Both Linux and Windows have a plethora of IPC mechanisms, most of which
are trivial to access from Tcl. Environment variables are just the wrong
tool for this.

I suggest writing values to a temporary file, a named pipe, a shared
memory segment, or a socket.

--
Darren New / San Diego, CA, USA (PST)
Remember the good old days, when we
used to complain about cryptography
being export-restricted?

0 new messages