std:env
The std:env module provides functions to load, read, and modify environment variables from Lua.
It supports loading variables from .env files, getting values, and setting new variables in memory and in the system environment.
Functions
env.load(filename: string): boolean
Loads variables from a .env file into memory and the system environment.
-
Parameters:
filename(string): path to the.envfile.
-
Returns:
trueif the file was loaded successfully.
Example:
local env = require("std:env")
-- Load variables from .env file
local ok = env.load(".env")
if ok then
print("Variables loaded successfully")
end
env.get(key: string): string?
Retrieves the value of a loaded environment variable.
-
Parameters:
key(string): the name of the variable.
-
Returns:
- (string?) the variable value if it exists, or
nilotherwise.
- (string?) the variable value if it exists, or
Example:
local env = require("std:env")
-- Get API key
local api_key = env.get("API_KEY")
print("API_KEY:", api_key)
-- API_KEY: 1234567890abcdef
env.set(key: string, value: string): boolean
Sets or updates an environment variable in memory and in the system environment.
-
Parameters:
key(string): the name of the variable.value(string): the value to assign.
-
Returns:
trueon success.
This does not modify the
.envfile on disk; the change exists only during program execution.
Example:
local env = require("std:env")
-- Define or update a variable
env.set("DEBUG", "true")
print("DEBUG:", env.get("DEBUG"))
-- DEBUG: true
Notes
- Use
.envfiles to manage configuration variables for your application. load()automatically updates the system environment (os.setenv).set()is JIT: changes last only during the program runtime. For persistent changes, you need to modify the.envfile manually.