loadenv

Loads environment variables from a .env file.

Category: Environment and Settings > System Commands

Syntax

loadenv(filename)

Reads NAME=value lines from filename and sets them as environment variables.

env = loadenv(filename)

Also returns the variables as a dictionary.

Description

A .env file lists one variable per line as NAME=value. Values may be quoted to keep spaces. After loadenv, the variables are visible to getenv and to commands started with system. This keeps settings such as paths or keys out of the source code.

Input arguments

filename
Path of the file to read. A missing file is an error.

Output arguments

env
A dictionary mapping each variable name to its value.

Example

Load a small file

fid = fopen('demo.env', 'w');
fprintf(fid, 'MYV1=abc\nMYV2="x y"\n');
fclose(fid);
loadenv('demo.env');
disp(getenv('MYV1'))
disp(getenv('MYV2'))
delete('demo.env')
Output
abc
x y

See also

getenv setenv isenv fopen