Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Environment Variable 1

Status
Not open for further replies.

gonzilla

Programmer
Apr 10, 2001
125
US
Hi,

I'm trying to write a program that will change a certain environment variable. I've tried using _putenv(). I realize that this just writes it for the current process but doesn't actually set it at the system level. I can tell becuase I will call putenv() and then use getenv() in the same program and it is fine, but it is gone after it exits.

Is there a way to get it to be set and stay? Basically, just as if I had just typed...

setenv VARNAME VALUE

...at a UNIX prompt.

Thanks.

-Tyler -Tyler
 
Hi:

In the Unix environment, any process spawned by the parent inherits the parent's environment so any changes made to the child are lost when the child terminates.

If you're using Bourne or Korn shell, to change the parent, use the dot operator. For example, if you have a file, called setenv, which contains:

PATH=$PATH:/usr/local/bin

do this:

. setenv # space after the dot

This changes your parent shell. If you have a "C" program that changes the environment this should work:

. whateveryourcprogramis

If you use csh, there's also a way to do it, but memory escapes me. I believe there's a thread in the Unix Shell Scripting forum which covers this topic.

Regards,


Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top