Main Content

loadenv

Load environment variables from .env and plain text files

Since R2023a

Description

example

loadenv(filename) loads environment variables from .env and plain text files by parsing one key-value pair per line and sets them as environment variables in the MATLAB® environment.

example

loadenv(filename,Name=Value) sets environment variables with additional options specified by one or more name-value arguments.

example

D = loadenv(filename) creates a dictionary from the key-value pairs. loadenv does not modify the MATLAB environment when an output argument is specified.

example

D = loadenv(filename,Name=Value) sets variables with additional options specified by one or more name-value arguments.

Examples

collapse all

Load the key-value pairs from a file to the MATLAB environment.

loadenv("samplefile.env")

Load the key-value pairs from a file to a dictionary.

D = loadenv("samplefile.env")

Create a file containing key-value pairs to use as environment variables.

keyvalues=["Key1='Value1'";"Key2='Value2'"];
writelines(keyvalues,"C:\Users\username\Desktop\samplefile.txt");

Load the key-value pairs to the MATLAB environment using a .env file type.

loadenv("samplefile.txt",FileType="env")

Create a .env file containing a pair of AWS access keys as key-value pairs.

keyvalues=["AWS_ACCESS_KEY_ID='my-aws-access-key-id-goes-here'";...
     "AWS_SECRET_ACCESS_KEY='my-aws-secret-access-key-goes-here'"];

writelines(keyvalues,"C:\Users\username\Desktop\s3secrets.env");

Load the key-value pairs from the file to the MATLAB environment.

loadenv("s3secrets.env")

View your environment variables.

getenv(["AWS_ACCESS_KEY_ID";"AWS_SECRET_ACCESS_KEY"])
ans = 

  2×1 string array

    "my-aws-access-key-id-goes-here"
    "my-aws-secret-access-key-goes-here"

Load the key-value pairs from a .env file into a dictionary.

D = loadenv("secrets.env")
D =

 dictionary (string ⟼ string) with 11 entries:

    "AWS_ACCESS_KEY_ID"     ⟼ "your-AWS_ACCESS_KEY_ID-goes-here"
    "AWS_SECRET_ACCESS_KEY" ⟼ "your-AWS_SECRET_ACCESS_KEY-goes-here"
    "MW_WASB_SAS_TOKEN"     ⟼ "your-MW_WASB_SAS_TOKEN-goes-here"
    "username"              ⟼ "your-username-goes-here"
    "password"              ⟼ "your-password-goes-here"
    "DB_NAME"               ⟼ "MySQL_DB"
    "DB_USER"               ⟼ "user_name"
    "DB_PASSWORD"           ⟼ "pass_word"
    "DB_DOMAIN"             ⟼ "abc123.mysqldb.com"
    "DB_PORT"               ⟼ "1234"
    "TEMPORARY_DOWNLOAD"    ⟼ "C:/Users/username/Local/Temp/my_download"

Lookup a value in your dictionary.

D("DB_USER")
ans =

   "user_name"

Input Arguments

collapse all

Name of the file to read, specified as a string scalar or character vector. The file must contain the key-value pairs to set as variables using the key=value format, such as:

AWS_ACCESS_KEY_ID="my-aws-access-key-id-goes-here"
AWS_SECRET_ACCESS_KEY="my-aws-secret-access-key-goes-here"
username="your-username-goes-here"
The .env file format is the recommended plain text file format for loadenv. A .env file (dotenv) is a plain text file containing keys and their corresponding values that can be loaded into the MATLAB environment. By using a .env file you can separate sensitive configuration data from code and provide different sets of configurations for different workflows.

Depending on the location of your file, filename can take on one of these forms.

Location

Form

Current folder or folder on the MATLAB path

Specify the name of the file in filename.

Example: "myFile.env"

File in a folder

If the file is not in the current folder or in a folder on the MATLAB path, then specify the full or relative path name in filename.

Example: "C:\myFolder\myFile.env"

Example: "dataDir\myFile.env"

Internet URL

If the file is specified as an internet uniform resource locator (URL), then filename must contain the protocol type "http://" or "https://".

Example: "http://hostname/path_to_file/my_data.env"

Remote Location

If the file is stored at a remote location, then filename must contain the full path of the file specified with the form:

scheme_name://path_to_file/my_file.ext

Based on the remote location, scheme_name can be one of the values in this table.

Remote Locationscheme_name
Amazon S3™s3
Windows Azure® Blob Storagewasb, wasbs
HDFS™hdfs

For more information, see Work with Remote Data.

Example: "s3://bucketname/path_to_file/my_file.env"

While parsing the file, loadenv will read one key-value pair per line as well as:

  • Skip empty lines and treat all contents of a line after # as a comment.

  • Interpret key names that consist only of letter, number, underscore, dot, and hyphen characters.

  • Preserve text in quotes without adjustment, trim whitespace outside of quotes, and expand new lines from double quoted values.

  • Read empty values as empty strings.

Note

Security Considerations: Because .env files are plain text, ensure the location and permissions of your .env file reflect the security level of your workflow:

  • Your local machine is often the most secure place to store your file.

  • During code deployment, do not deploy a .env file containing your credentials.

  • Do not check your .env files into source code repositories. For example, in your repositories, include the .env file extension in your .gitignore_global file to exclude .env files from all your repositories. For more information on how to configure Git to ignore files, see the GitHub page Ignoring Files.

Data Types: string | char

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: loadenv(filename,OverwriteEnvironmentVariable=true) overwrites existing environment variables.

Overwrite existing environment variables, specified as a numeric or logical 1 (true) or 0 (false).

  • If true, loadenv will overwrite existing environment variables of the same name.

  • If false, loadenv will skip overwriting existing environment variables when reading the input file.

Example: OverwriteEnvironmentVariable=true

Expand variables, specified as a numeric or logical 1 (true) or 0 (false). If true, loadenv will apply variable expansion in the form ${VAR} where VAR is the variable to be replaced. If false, loadenv will not apply any variable expansion.

When expanding variables, loadenv seeks the variable value first in the input .env file, then the user environment. If the variable value is not found in either location, a default value can be specified as ${VAR:-Default}. If none of these apply, loadenv reads the variable value as empty.

Example: ExpandVariables=true

Character encoding scheme associated with the file, specified as "auto", "system", or a standard character encoding scheme name.

  • If specified as "auto", loadenv will detect encoding from the file.

  • If specified as "system", loadenv will use your system's default encoding.

Example: Encoding="UTF-8" uses UTF-8 as the encoding.

Example: Encoding="system" uses the system default encoding.

Data Types: char | string

Type of file, specified as one of these values:

  • "auto" — Automatically detect the file format of the input file from the extension specified in filename.

  • "env" — Read the contents of the input file as .env. If the file extension in filename is not .env, you can specify the value of FileType as "env" to read the contents of the input file as a .env file.

Example: FileType="env"

Output Arguments

collapse all

Output dictionary, returned as a dictionary of strings. A dictionary is a map that stores data as values, which can be indexed using corresponding unique keys. For more information on dictionaries, see dictionary.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2023a

See Also

| | |