Unset environment variable

Hello, everybody :wave:

Is it possible somehow to unset (remove) environment variable after it was set with Unix.putenv? Maybe I miss something, but I do not see any suitable function for this task in Stdlib :frowning:

2 Likes

Indeed, I believe this function does not exist in the standard library at the moment. It could be a nice addition.

Cheers,
Nicolas

  1. thereā€™s a unsetenv(3) function, so unless thereā€™s a good argument, I donā€™t quite see why there isnā€™t a wrapper in the unix library. But maybe Iā€™m missing something.
    2. I wanted to suggest that you could just supply a full environment to unix execve, but then, it seems thereā€™s no way to get a full environment list either (?)

Yeah, this seems like a real problem. Also, really easy to fix. Unless somebody else wants to just hack up a patch, Iā€™ll do right now. Maybe Iā€™ll learn that thereā€™s an incompatibility issue I donā€™t understand. Hey, itā€™ll be an adventure!

Oh, thereā€™s Unix.environment() which returns the environment, and then you can pass it to Unix.execve(). I retract my previous comment.

ETA: I donā€™t mean to suggest that this is a solution to your problem, but rather, a workaround.

FWIW there is (among others) unsetenv in ExtUnix.

With the advent of OCaml 5.0, adding a thread-broken function to the standard library seems a bit short-sighted. In fact, it might be worth considering the removal of Unix.putenv altogether.

The majority of uses of putenv/unsetenv can be adequately replaced by calls to execve. The only use case that is not covered by execve, as far as I know, is when you want to communicate with a linked library through the putenv/getenv covert channel.

And the use-case of getting times in different timezones by setting the TZ variable.

3 Likes

There was an attempt to add Unix.unsetenv together with Unix.setenv, but it was not merged because of concerns about the semantics of Unix.setenv. IMO Unix.unsetenv by itself should be uncontroversial.