A C program/library to read environment variables from a .env file, similar to other packages/libraries available in various programming languages.
- To use dotenv library in your project simply add the
dotenv.c
anddotenv.h
files in your project along with your.env
file as shown below or create a shared library as per your need:
├── .env
├── dotenv.c
├── dotenv.h
└── main.c
- Once
dotenv.h
is included in your file, invoke theload_env
function which takes filepath of the.env
file as an argument:
// filename: main.c
// other include statements
#include <stdlib.h>
#include "dotenv.h"
int main()
{
load_env(".env"); // if the .env file is in the current directory.
// use the getenv method in the <stdlib.h> to get the value of your environment variable.
char myVar[] = getenv("myVar");
// other logic...
}
- Your
.env
file can look something like this:
myVar="myVarValue"
myvar = "myvarvalue"
#mycommentedvar = "mycommentedvarvalue"
- Then compile you're program using
gcc
or similar compiler:
gcc main.c dotenv.c -o main
- Or if you created a shared library:
gcc main.c -ldotenv -o main # or whatever you named your library