4

Is there a way to set an environment variable on a Unix/Linux machine (not process) using .NET Core?

I noticed that I can use Environment.SetEnvironmentVarible for Windows, however, after searching I haven't found an equivalent for a Unix/Linux system.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrew Blanchette
  • 303
  • 1
  • 3
  • 7

1 Answers1

5

Environment.SetEnvironmentVariable also works on Linux, etc.

But be beware only the current process environment variables are set, and not machine or user environment variables like on Windows:

On .NET Core on macOS and Linux systems, calls to the SetEnvironmentVariable(String, String, EnvironmentVariableTarget) method with a value of EnvironmentVariableTarget.Machine or EnvironmentVariableTarget.User are ignored.

From Environment.SetEnvironmentVariable Method.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Julian
  • 33,915
  • 22
  • 119
  • 174
  • I'm actually looking for a way to set the machine variable and not the process variable. – Andrew Blanchette Jun 17 '19 at 17:05
  • 4
    There's no separation of Process vs User vs Machine variables on Linux. All environment variables are treated the same. And a process can only change the environment variables of itself and any children it spawns. – omajid Jun 17 '19 at 19:22