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.

1 Like

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.