Linux环境变量getenv setenv unsetenv操作

Linux环境变量getenv setenv unsetenv操作

在linux操作系统中,可以通过man来进行相关函数的查看 eg: man setenv

1、setenv 设置环境变量

//函数原型
int setenv(const char *name, const char *value, int overwrite);

1.return value 返回值

setenv() function returns zero on success, or -1 on error, with er‐rno set to indicate the cause of the error.

The  unsetenv()  function returns zero on success, or -1 on error, witherrno set to indicate the cause of the error.

2、getenv 获取环境变量

//函数原型
char *getenv(const char *name);

2.return value 返回值

The getenv() function returns a pointer to the value in the environment, or NULL if there is no match.

3、unsetenv 删除环境变量

//函数原型
int unsetenv(const char *name);

3.return value 返回值

The setenv() function returns zero on success, or -1 on error, with errno  set  to  indicate the cause of the error.

The  unsetenv() function returns zero on success, or -1 on error, with errno set to indicate the cause of the error.

实验测试

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define ENV_NAME  "LINUX"
int main(void)
{
    char* val;    
    //获取Linux环境变量的值
    val = getenv(ENV_NAME);
    printf("No.1 %s=%s\n", ENV_NAME, val);

    //覆盖写入环境变量
    setenv(ENV_NAME, "I Can Get It", 1);
    printf("No.2 %s=%s\n", ENV_NAME, val);
    
    val = getenv(ENV_NAME);
    printf("No.3 %s=%s\n", ENV_NAME, val);
  
    //删除一个环境变量
    int ret =unsetenv(ENV_NAME);
    printf("ret =%d\n",ret);
    
    val = getenv(ENV_NAME);
    printf("No.3 %s=%s\n",ENV_NAME, val);    
    return 0;
}
 

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小殷学长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值