Pejman Moghadam / C-programming

C - Changing environment variables

Public domain


int main ()
{
  int overwrite = 1;
  char *test;

  test = getenv("TEST");
  printf("TEST environment variable is %s \n",
      (test != NULL) ? test : "undefined.");

  setenv ("TEST", "102030", overwrite);

  test = getenv("TEST");
  printf("TEST environment variable is %s \n",
      (test != NULL) ? test : "undefined.");

  unsetenv("TEST");

  test = getenv("TEST");
  printf("TEST environment variable is %s \n",
      (test != NULL) ? test : "undefined.");
}

Output

$ export TEST="hello"

$ ./a.out 
TEST environment variable is hello 
TEST environment variable is 102030 
TEST environment variable is undefined. 

$ echo $TEST
hello

BY: Pejman Moghadam
TAG: c, setenv, unsetenv, environment
DATE: 2011-06-02 18:26:42


Pejman Moghadam / C-programming [ TXT ]