
Nim: How to read environment variables
Nim: How to read environment variables
Reading environment variables is a simple way to enable runtime configuration of an application or script. Within Nim there’s two functions to get familiar with for reading values from the environment.
if getEnv("MY_VAR") == "1":
echo "Got it!"
else:
echo "Nope :("
This gets the value of MV_VAR
(the equivalent of $MY_VAR
from your shell). If the value is unset getEnv
will return an empty string. Or, alternatively, you can provide a second argument to getEnv
to specify the default value to us if your variable is unset:
if getEnv("MY_VAR", "is unset") == "is unset":
echo "Nope :("
If you need to differentiate between a truly unset value and one that has inherited the default value then there is existsEnv
:
if existsEnv("MY_VAR"):
echo "This is set"
else:
echo "This is not set"
Published: 21/03/2022
Hi, I'm Glenn! 👋
I've spent most of my career working with or at startups. You'll usually find me working in Product leadership roles, on an advisory board, or maybe as an early investor.
I've been the VP of Product & GTM @ Ockam. I led the Terraform product team @ HashiCorp, where we launched Terraform 1.0, Terraform Cloud, and a whole host of amazing capabilities that set the stage for a successful IPO. Prior to that I was part of the Startup Team @ AWS, and earlier still an early employee @ Heroku. I've also invested in a couple of dozen early stage startups.
I've spent most of my career working with or at startups. You'll usually find me working in Product leadership roles, on an advisory board, or maybe as an early investor.
I've been the VP of Product & GTM @ Ockam. I led the Terraform product team @ HashiCorp, where we launched Terraform 1.0, Terraform Cloud, and a whole host of amazing capabilities that set the stage for a successful IPO. Prior to that I was part of the Startup Team @ AWS, and earlier still an early employee @ Heroku. I've also invested in a couple of dozen early stage startups.