Steve Holden steve at holdenweb.com
Tue Dec 20 16:27:50 CET 2005

Mike Meyer wrote:
> Terry Hancock <hancock at anansispaceworks.com> writes:
>>On Tue, 20 Dec 2005 05:35:48 -0000
>>Grant Edwards <grante at visi.com> wrote:
>>>On 2005-12-20, dpugmire at gmail.com <dpugmire at gmail.com>
>>>>I have csh script that calls a bunch of python programs
>>>>and I'd like to use env variables as kind of a global
>>>>variable that I can pass around to the pythong scripts.
>>>You can't change the environment of the parent process.
>>>IOW, the python programs can't change the environment of
>>>the calling csh script (or, by extenstion, the environment
>>>of subsequent python programs that are called by the csh
>>There is an evil trick, however:
>>Instead of setting the environment directly, have the python
>>program return csh code to alter the environment the way you
>>want, then call the python code by "sourcing" its output:
>>source `my_script.py`
> Does this actually work? It looks to me like you need two levels:
> my_script.py creates a file, then outputs the name of the file, as the
> csh source command reads commands from the file named as an argument.
> To be able to output the commands directly, you'd need to use the eval
> command, not the source command.
>>It's ugly, but it does work -- I have had to use this
>>before in a production environment.  Well, it's not really
>>any less advisable than scripting in csh to begin with. ;-)
> Doesn't matter what you're scripting in - you'll have to do some such
> circumlocution to set the parent scripts environment variables.

I suspect the trick that Terry was thinking of was eval, not source. You 
are correct in saying he'd need to create a file to source.

When I run


I see:

SSH_AUTH_SOCK=/tmp/ssh-J0r9XFySTm/agent.5500; export SSH_AUTH_SOCK;
echo Agent pid 5796;

The process number varies each time. If I run

     eval `ssh-agent`

I see:

Agent pid 4364

(this is Cygwin, hence the funky process numbers). Now, of course, I can 
see the variables in the current shell's environment:

sholden at bigboy /tmp
/tmp/ssh-W6LEPi8wC0/agent.4152 4364

Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/

More information about the Python-list mailing list