Environment variable operation function

getenv environment variable value acquisition function;

setenv change or add an environment variable function;

unsetenv cancel environment variable;

(Function can obtain detailed information on the terminal function name in man)

#include <stdio.h> 
#include <stdlib.h> 
#include < String .h> int main () 
{ char * Val;
     const char * name = " the ABD " ; 
    Val = getenv (name);   // Get name environment variable value 
    the printf ( " . 1,% S =% S \ n- " , name, Val); 
    setenv (name, " haha-Day-and-night " , . 1 ); // set the environment variable is the name haha and-night--day 
    Val = getenv (name); 
    the printf ( "


     
    

2,%s = %s\n",name,val);

#if 0
    int ret = unsetenv("ABCDEFG");
    printf("ret = %d\n",ret);
    
    val = getenv(name);
    printf("3,%s = %s\n",name,val);
#else
    int ret = unsetenv("ABD");  //取消环境变量name的值
    printf("ret = %d\n",ret);

    val = getenv(name);
    printf("3,%s = %s\n",name,val);
#endif
    return 0;
}    

operation result:

ubuntu1604@ubuntu:~/wangqinghe/C/20190805$ gcc setenv.c -o setenv

ubuntu1604@ubuntu:~/wangqinghe/C/20190805$ ./setenv

1,ABD = (null)

2,ABD = haha-day-and-night

K = 0

3,ABD = (null)

 

The if 0 to 1

operation result:

ubuntu1604@ubuntu:~/wangqinghe/C/20190805$ gcc setenv.c -o setenv

ubuntu1604@ubuntu:~/wangqinghe/C/20190805$ ./setenv

1,ABD = (null)

2,ABD = haha-day-and-night

K = 0

3,ABD = haha-day-and-night

 

No ABCDEFG this environment variable when you remove it, is not being given.

 

However, if the int ret = unsetenv ( "ABCDEFG");

Instead int ret = unsetenv ( "ABD ="); this time, it will get an error.

 

operation result:

ubuntu1604@ubuntu:~/wangqinghe/C/20190805$ ./setenv

1,ABD = (null)

2,ABD = haha-day-and-night

K = -1

3,ABD = haha-day-and-night

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/11304929.html