setenv
表头文件 #include<stdlib.h>
定义函数 int setenv(const char *name,const char * value,int overwrite);
函数说明 setenv()用来改变或增加环境变量的内容。参数name为环境变量名称字符串。参数 value则为变量内容,参数overwrite用来决定是否要改变已存在的环境变量。如果overwrite不为0,而该环境变量原已有内容,则原内容会被改为参数value所指的变量内容。如果overwrite为0,且该环境变量已有内容,则参数value会被忽略。返回值 执行成功则返回0,有错误发生时返回-1。
错误代码 ENOMEM 内存不足,无法配置新的环境变量空间
范例
#include<stdlib.h>
main()
{char * p;
if((p=getenv(“USER”)))
printf(“USER =%s
”,p);
setenv(“USER”,”test”,1);
printf(“USER=%s
”,getenv(“USEr”));
unsetenv(“USER”);
printf(“USER=%s
”,getenv(“USER”));
}
执行 USER = rootUSER = testUSER = (null)
注释:stdlib.h 在Linux和Windows里略有不同,比如setenv函数是用在Linux里的,而在Windows里则没有setenv函数,可用putenv来代替。
____________________________________________________
Linux命令:setenv
功能说明:查询或显示环境变量。
语法:setenv [变量名称][变量值]
补充说明:setenv为tsch中查询或设置环境变量的指令。
英文版本,欢迎翻译:
ABOUT SETENV
In C shell sets the value of an environment variable.
SYNTAX
setenv [var [word]]
var Variable of the set command. word Set instructions or information of the variable.
EXAMPLES
setenv PATH "/bin:/usr/bin:/usr/sbin:ucb/bin" - Sets the environment path to search for files in the /bin, /usr/bin, /usr/sbin and usb/bin directory.