How to modify process environment variables

Ky-Anh Huynh saigon at example.net
Wed Oct 18 01:28:57 UTC 2017


On Tuesday, 17 October 2017 at 11:49:32 UTC, Jacob Carlborg wrote:
> On 2017-10-17 06:51, Ky-Anh Huynh wrote:
>> Hi,
>> 
>> Is it possible to change the current process's environment 
>> variables?
>> 
>> I have looked at `std/process.d` source code, and there is 
>> only a private method `createEnv` used when new (sub)process 
>> is created.
>> 
>> In C `putEnv` the answer is positive: 
>> http://man7.org/linux/man-pages/man3/putenv.3.html (FIXME)
>> 
>> I come to this question as I want to set some custom variables 
>> for my unittests. My program reads some tokens from system 
>> environments, and it's convenient if I can simulate different 
>> cases for testings.
>> 
>> Thanks for your reading and support.
>
> Use std.process.environment [1] and assign to it like an 
> associative array.
>
> [1] 
> https://dlang.org/phobos/std_process.html#.environment.opIndexAssign

Oh thanks a lot for your pointing out, Jacob. That's the thing 
I'm looking for. The C version is not so bad though

```
   import core.sys.posix.stdlib;
   import std.string: toStringz;

   string jenkinsToken = "TEST_TOKEN=";
   putenv(cast(char*)jenkinsToken);
```



More information about the Digitalmars-d-learn mailing list