[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: How to modify evironment variables in a C program

On Fri, Mar 08, 2002 at 06:23:15PM -0600, Xanh wrote:
> Hello,
> How do I export a change to an environment variable made in a C program?
> I tried setenv() and putenv(), but the change is not visible to the shell.
> ////////// testenv.c
> int main(int argc, char* argv[], char* envp[])
> {
>         char* p;
>         p = getenv("MyTest");
>         printf("current value: %s\n", p);
>         setenv("MyTest", "New value here", 1);
>         p = getenv("MyTest");
>         print("new value: %s\n", p);
>         return 0;
> }
> /////////

This changes the environment variable just fine. If you subsequently (in
the same program) did something like this (untested):

	system ("echo $MyTest");

you'd see it is set just fine.

The reason you don't see it being set when you run your program from
a shell is because environment variables are an attribute of the shell/
program and are visible only to THAT program or its children. You cannot
set the environment of any level of parent program. 

> I compiled the code and test in a bash shell:
> $>export MyTest="Current Value"
> $>./testenv
>     current value: Current Value
>     new value: New value here
> $>echo $MyTest
>     Current Value
> The old value is retained.
> Thanks for your help.
> Regards,
> Nemo Mai
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
> _______________________________________________
> Redhat-list mailing list
> Redhat-list redhat com
> https://listman.redhat.com/mailman/listinfo/redhat-list

---- Fred Smith -- fredex fcshome stoneham ma us -----------------------------
  "And he will be called Wonderful Counselor, Mighty God, Everlasting Father,
  Prince of Peace. Of the increase of his government there will be no end. He 
 will reign on David's throne and over his kingdom, establishing and upholding
      it with justice and righteousness from that time on and forever."
------------------------------- Isaiah 9:7 (niv) ------------------------------

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]