通过C程序更改环境变量 [英] Changing a environmental variable through a C program

查看:123
本文介绍了通过C程序更改环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何通过C程序更改Linux环境变量?我不想更改为执行该程序而复制的环境变量.我希望能够从C程序中更改其值,然后在linux中执行命令"env"时,我可以看到其值已更改.

Anyone knows how can I change the Linux environmental variables through a C program? I dont want to change the environmental variables that are copied for the execution of that program. I want to be able to change its value from a C program and then when executing the command 'env' in linux I can see its value changed.

有什么提示吗?

推荐答案

我不想更改要复制的环境变量 该程序的执行.我希望能够改变它的价值 从C程序,然后在Linux中执行命令"env"时 可以看到它的值改变了

I dont want to change the environmental variables that are copied for the execution of that program. I want to be able to change its value from a C program and then when executing the command 'env' in linux I can see its value changed

不能. 您只能更改自己的流程环境.您无法触及父母的环境.换句话说,您所做的任何事情(设置/清除环境变量,更改本地目录等)对于父进程都是不可见的.

You can't. You can only change the environment of your own process. You have no way of ever touching the environment of the parent. Put another way, anything you do (setting / clearing environment variables, changing local directory, etc.) will be invisible for the parent process.

该标准明确规定:

setenv()函数应在以下位置更新或添加一个变量: 呼叫过程的环境.

The setenv() function shall update or add a variable in the environment of the calling process.

改变父母环境的唯一方法是自己动手做.

The only way to change the environment of the parent is to get it to do it himself.

这篇关于通过C程序更改环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆