Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
986 views
in Technique[技术] by (71.8m points)

c - Why the environment variable is unset after using setenv( )

I wrote a C program in Linux to set the values of environment variables using setenv, but after execution, when I run set or export, the environment variable itself seems to be unset. Why?

Here is the code snippet:

int main()
{
  char *mallocPtr, *callocPtr, *reallocPtr, *memalignPtr, *vallocPtr;
  struct sigaction sa;

  sa.sa_handler=SIGSEGV_handler;
  sigaction(SIGSEGV, &sa, NULL);

  if(setenv("ENV1", "3", 1) == 0)
         printf("ENV1 set to 3
");
  else
         fprintf(stderr, "setenv failed on ENV1");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The environment variables are set within the context of your program.

When your program exits, you're back in the context from where your program was started.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...