Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...