.NET 11 Preview - Passing Environment Variables
[C#, .NET, .NET 11 Preview]
In a previous post, “Overriding appsettings.json via Environment Variables”, we looked at how to access and override environment variables in our applications.
Take the following simple program that has the following code:
Console.Write(Environment.GetEnvironmentVariable("HOME"));
If you run this, it will print your home directory:

If you wanted to change this for testing purposes, you would typically do it like this;
export Home=/Users/Conrad
And then we would re-run our program to pick up the new values.
This tends to be pretty cumbersome for a couple of reasons:
- You affect other applications and tools running that might use this value
- It can get monotonous if you do this a lot
.NET 11 has a solution for this - you can pass the value you want accessed in the command like using the -e argument of the dotnet run tool, with the name of the variable you want to set followed by its value.
dotnet run -e NAME=VALUE
So we can simplify our debug and runtime experience as follows:
dotnet run -e HOME=/Users/Conrad
This will print the following:

TLDR
You can pass environment variables you want your application to use using the -e argument.
The code is in my GitHub.
Happy hacking!