Skip to content

Command-line tool

adotenv-cli checks, formats, compares and loads .env files from a shell or a CI job. It uses the library APIs, so it reads files exactly as the library does.

Terminal window
cargo install adotenv-cli --locked

This installs one binary, adotenv-cli. It needs Rust 1.81 or later to build. adotenv-cli --help prints the usage and adotenv-cli --version prints the version.

adotenv-cli [--dialect <portable|node|python|compose|docker>] [--dialect=<value>]
[--max-input-bytes <n>] [--max-lines <n>]
[--max-items <n>] [--max-diagnostics <n>] <command> [options]
get <file> <key>
check [--format human|json|github] [--json] [--source] [-r|--recursive] [--exclude <path>] <path>...
fmt [--write|--check] [--sort] [--max-output-bytes <n>] [--max-output-lines <n>] [--max-references <n>] [-r|--recursive] [--exclude <path>] <path>...
diff [--format human|matrix|json] [--json] [--mask] <baseline> <file>...
example <file>
load <directory> [mode]
set [--max-output-bytes <n>] [--max-output-lines <n>] <file> <key> <value>
run <directory> [mode] -- <program> [args...]
Command Does
get Prints the effective value of a key and a line break, without quoting.
check Reports diagnostics for each file.
fmt Prints formatted text, checks formatting with --check or rewrites files with --write. --sort also sorts each group.
diff Compares each file with the baseline, the first path.
example Prints the document with assigned values removed.
load Prints the merged cascade of a directory as Portable lines.
set Changes the value of a key in place.
run Loads a cascade and runs a program with the loaded values.

set changes an existing declaration. The key must be declared exactly once; a missing or duplicated key is an error, and set never adds a key. When the value is already current, set leaves the file untouched.

--json is short for --format json, --format=<value> is accepted, and -r is short for --recursive. Two different formats in one command are a usage error, as is --source with JSON or GitHub output, and --write with --check. Arguments must be valid UTF-8.

--dialect selects the reading rules for every file in the command. It can appear before the command, or anywhere after the command name before --. For set, a --dialect token after the file argument is a key or value rather than a global option; put the global option earlier or use --. The --dialect=<value> form is equivalent and may be specified only once. Unknown options are usage errors rather than paths, unless they follow --.

Value Library preset
portable (default) Dialect::PORTABLE
node Dialect::NODE_DOTENV
python Dialect::PYTHON_DOTENV
compose Dialect::COMPOSE
docker or docker-raw Dialect::DOCKER_RAW

See Dialects for what each preset accepts.

--max-input-bytes <n>, --max-lines <n>, --max-items <n> and --max-diagnostics <n> (or their = forms) are global options accepted in the same positions as --dialect and specified at most once. They forward ParseOptions::with_max_input_bytes, ParseOptions::with_max_lines, ParseOptions::with_max_items and ParseOptions::with_max_diagnostics to parsing and loading. check reports limit violations as dotenv.limit-exceeded diagnostics (exit code 1); strict commands (get, fmt, diff, example, load, set, run) exit with code 2. Consistent with ParseOptions::max_diagnostics, --max-diagnostics applies to diagnostic-collecting parses (check, fmt, example, and set edit verification); strict/values-only commands (get, diff, load, run) keep only the first error and do not build a diagnostic list.

fmt also accepts --max-output-bytes <n>, --max-output-lines <n> and --max-references <n> (and their = forms, each at most once) to bound sorted and formatted output through EditOptions (Document::sort_with_options and Document::format_with_options); --max-references caps reference scanning during fmt --sort.

set also accepts --max-output-bytes <n> and --max-output-lines <n> (and their = forms, each at most once) before <file> to bound the edited file through EditOptions before writing.

Values may be secrets. check prints source lines only with --source. These commands print values or text that can contain them:

  • get prints the effective value.
  • load prints every loaded value.
  • diff prints values in every format: human, matrix and JSON. diff --mask replaces compared values with ***.
  • fmt prints or writes the whole document.
  • example removes values but keeps comments verbatim, including anything secret written in a comment.

check prints one line per diagnostic: path, one-based line and column, severity, code and message. A stripped leading byte-order mark (or BOM run under BomPolicy::StripAll) is hidden on line 1, so it does not count in the human or GitHub column; BomPolicy::Keep and BomPolicy::Error keep it in column 1. JSON spans still count its bytes. A summary line follows when any file has a diagnostic or could not be read; a read failure counts as one error and one affected file. A clean run prints nothing.

$ adotenv-cli check bad.env
bad.env:1:1: error[dotenv.invalid-key]: key does not match the selected dialect
bad.env:3:1: warning[dotenv.duplicate-key]: duplicate key; lookup follows the selected dialect policy
1 error, 1 warning, 0 info in 1 file

--source adds the source line and a caret under the span. A leading byte-order mark is omitted from the source line. Human diagnostics go to standard output and read errors to standard error. JSON reports a read error as a files entry on standard output, and GitHub output emits an annotation there. Human output is not a machine contract; use JSON or GitHub output in scripts. The codes are listed in Diagnostic codes.

The report is JSON. The CLI emits one line; this example is formatted for readability:

{
"version": 1,
"files": [
{
"path": "app.env",
"ok": false,
"diagnostics": [
{
"line": 1,
"severity": "error",
"code": "dotenv.invalid-key",
"message": "key does not match the selected dialect",
"span": {
"start": 0,
"end": 7
},
"secondary_span": null
}
]
}
]
}

Every field is listed below, in output order. The fields, codes, severities and spans are stable.

Object Field Type Meaning
check report version number Schema version, 1.
check report files array of check file One entry per found file, in argument order. With --recursive, found files are sorted and duplicates resolved to the same path are removed. A path that could not be searched comes first, with ok:false and an error.
check file path string The path as given or found, the path that could not be searched, or - or --stdin for standard input. Bytes that are not UTF-8 become U+FFFD.
check file ok boolean false when the file has an error diagnostic, could not be read, or its search path failed.
check file error string Present only when the file could not be read, is not UTF-8, or its search path failed. The message includes the path.
check file diagnostics array of diagnostic The file’s diagnostics. Empty when error is present.
diagnostic line number One-based line of the primary span.
diagnostic severity string error, warning or info.
diagnostic code string Stable diagnostic code, such as dotenv.invalid-key.
diagnostic message string Human-readable text. Match on code instead.
diagnostic span span Primary source range.
diagnostic secondary_span span or null A related range, such as the first declaration of a duplicate key, or null.
span start number Byte offset into the file. A leading byte-order mark is counted.
span end number Byte offset just past the range.

A file with no diagnostics produces an empty diagnostics array. The shared JSON Schema is tests/vectors/schema/check-report.v1.json and is published at https://zeropier.com/adotenv/schema/check-report.v1.json.

One GitHub Actions workflow command per line:

::<level> file=<path>,line=<line>,col=<column>,title=<code>::<message>
::error file=<path>,title=adotenv::<error>

The first form reports a diagnostic. <level> is error, warning or notice for an info diagnostic. <column> is one-based and counts Unicode scalar values, not a leading byte-order mark. The second form reports a file that could not be read or a path that could not be searched. In <path> and <code>, %, CR, LF, : and , are percent-encoded. In <message> and <error>, %, CR and LF are percent-encoded. This format is stable.

- run: cargo install adotenv-cli --locked
- run: adotenv-cli check --format github --recursive

diff compares effective declarations. In human output, each candidate starts with a baseline -> file header, followed by one line per difference: - for a key that takes part in lookup only in the baseline, + for one that takes part only in the candidate and ~ for a changed value, state or export prefix. A value that contains a line break or lone CR can make that difference span more than one physical output line. A candidate with no differences prints (no differences). This applies when there is exactly one candidate as well.

$ adotenv-cli diff base.env app.env
base.env -> app.env
~ B: two -> changed
~ C: 3 (export) -> 3
- BARE=<bare>
+ D=""

--mask replaces every non-empty compared value with *** in human, JSON and matrix output. Keys, states, export prefixes and missing/unset markers remain visible. It affects output only; comparison still uses the real values.

--format matrix prints one table with a row per key and a column per file. A cell shows the value, "" for an empty value, <bare> for a declaration without =, <unset> for a missing key and an export prefix for exported declarations. Neither human output nor the matrix is a machine contract.

The report is JSON. The CLI emits one line; this example is formatted for readability:

{
"version": 1,
"baseline": "base.env",
"files": [
{
"path": "app.env",
"ok": true,
"differences": [
{
"key": "A",
"kind": "value",
"left": {
"state": "value",
"value": "1",
"exported": false
},
"right": {
"state": "value",
"value": "2",
"exported": false
}
}
]
}
]
}

Every field is listed below, in output order. The schema is stable.

Object Field Type Meaning
diff report version number Schema version, 1.
diff report baseline string The baseline path as given.
diff report files array of diff file One entry per candidate, in argument order.
diff file path string The candidate path as given.
diff file ok boolean Always true. A file that cannot be read or parsed stops diff with exit 2 and no JSON.
diff file differences array of difference Baseline keys in the order of their first declaration that takes part in lookup, then the keys that take part in lookup only in the candidate, in the same order. Keys whose declarations agree are left out.
difference key string The key.
difference kind string The first difference found, checked in this order: only-left (takes part in lookup only in the baseline), only-right (only in the candidate), state, value, export.
difference left value or null The baseline declaration, or null when the key is absent.
difference right value or null The candidate declaration, or null when the key is absent.
value state string bare for a key without =, empty for an empty value, otherwise value.
value value string or null The decoded literal value: "" for empty, null for bare, or "***" for a non-empty value under --mask.
value exported boolean Whether the declaration has an export prefix.

The shared JSON Schema is tests/vectors/schema/diff-report.v1.json and is published at https://zeropier.com/adotenv/schema/diff-report.v1.json.

fmt applies FormatOptions::normalize_groups():

  • one space on each side of =, so A=1 becomes A = 1, in dialects where that keeps the value;
  • trailing spaces and tabs removed;
  • each run of blank lines collapsed to one;
  • quote styles kept;
  • a final line break ensured.

--sort also sorts declarations within each blank-line group; see Formatting and sorting. Formatting never changes a value. A change that would alter meaning is refused and exits 2.

With several files, printed output starts each file with a ==> path <== header. --check prints path: would reformat for each file that would change and exits 1 if any would. --write rewrites changed files in place and leaves an unchanged file untouched. Diagnostics are reported on standard error, and a file with error diagnostics is still formatted.

load prints the merged cascade as a Portable document: one KEY=value line per key, sorted by key, with LF line endings. Values are quoted and escaped by the core Portable encoder, so the output parses back to the same keys and values with the Portable dialect, whichever dialect read the files. No value spans several lines.

When a key is not a Portable key, or a value contains a control character other than tab, CR or LF, load prints nothing and exits 2. The error names the key but not the value.

A cascade file that does not exist is skipped, so a directory without any cascade files, or one that does not exist, loads nothing: load prints nothing and exits 0.

run starts the program with the parent environment plus the loaded values. A loaded value replaces a parent variable of the same name. Other parent variables are passed through unchanged. The parent environment is not changed. When no cascade file exists, the program runs with the parent environment alone.

On Windows the child environment is case-insensitive: keys that differ only by case and values longer than 32,767 UTF-16 code units are rejected before the child starts. Loaded files are trusted like code. A loaded PATH changes which program runs, and variables such as LD_PRELOAD change how it runs.

Values stay literal: run does not expand references or run command substitutions. The loader omits empty keys; keys that remain and contain = or NUL, and values that contain NUL, are rejected before the program starts.

With --recursive, check and fmt search each directory argument for .env and .env.* files. With no path they search .. A file argument is included whatever its name, and - reads standard input.

Without --recursive, every path argument must be a file. A directory is a read failure, and check or fmt with no path is a usage error.

The search skips .git, target, node_modules and symbolic links. A directory argument that is a symbolic link is refused; a symbolic link that names a file is an explicit file argument and is followed once.

A path that cannot be searched, such as a missing root, a refused symbolic link or an unreadable directory, is reported. Human output writes it to standard error, JSON adds a files entry with ok:false and error, and GitHub output writes an ::error annotation. The remaining paths are still searched and processed, and the command exits 2.

When the search finds no files and no path failed, check prints an empty report ({"version":1,"files":[]} for JSON), no .env files found goes to standard error, and the exit code is 0. fmt behaves the same way.

--exclude <path> needs --recursive and can be repeated. Each exclude is relative to each directory argument, or to the parent directory of a file argument. It removes the path and everything below it, comparing whole path components. A leading ./ is ignored and an empty exclude has no effect. An absolute exclude is compared with paths as they are spelled, so it only matches below an absolute directory argument.

File-reading commands accept - or --stdin for standard input, at most once per command. set and fmt --write need a file and refuse standard input. load and run also refuse standard input because they take a directory. -- ends option parsing for check, fmt, diff, get, example, load and set, so a leading-dash path can be named explicitly; use it before the first path argument. For run, -- separates the directory and optional mode from the program and its arguments.

When the result is unchanged, fmt --write and set do not replace the file. Its inode, hard links, modification time, permissions and extended attributes stay as they are.

fmt --write and set resolve the write target once before reading and do not resolve it again while replacing it. On Unix they check its device, inode, size and modification time before the temporary file and immediately before the replace, and fail if it changed after the read. They replace a changed file through a temporary file in the same directory:

  • When the path is a symbolic link, the file it finally points to is replaced in that file’s directory, and the link is kept.
  • A path that is a directory or another non-regular file is refused.
  • Hard links are not kept. The path gets a new file, and other links to the old file keep the old contents.
  • Extended attributes and ACLs are not copied.

On Unix the temporary file is created with mode 0600 before anything is written. It then receives the original owner, group and mode, is synced and is renamed over the original. The directory is synced after the rename. The original owner is kept when the running user may set it, for example as root; otherwise the new file belongs to the running user. The group is kept when the running user may set it. If it cannot be kept and the mode grants the group any access, the write fails and the file is unchanged.

On Windows the command writes a temporary file, applies the original permissions and attempts a direct replace. If that fails, it moves the old file to a backup, installs the new file and restores the backup on error. The Windows fallback is not crash-atomic.

Every command except run exits with one of these codes:

  • 0: success.
  • 1: findings. check or fmt found an error diagnostic, fmt --check found a file that would change, diff found a difference, or get did not find the key.
  • 2: the command could not complete. This covers usage errors, files that cannot be read or are not UTF-8, failed writes, and input the command cannot process: an invalid document for get, diff, set, load or run, an edit that set cannot apply, load output that has no Portable spelling, or a fmt change that would alter meaning. Failed writes include writes to standard output and standard error. A closed output pipe also exits 2, without a message.

check and fmt process every file and exit with the highest code, so a file that fails takes precedence over findings in other files.

Warning and info diagnostics do not change the exit code. example reports non-limit diagnostics on standard error and exits 0, or exits 2 without output when a parse limit is exceeded.

run exits 2 for a usage error, when it cannot load the cascade, when a loaded key or value cannot be passed to a child, or when the program cannot be started. Otherwise it exits with the child’s exit code. On Unix, a child ended by signal N exits 128 + N, as in POSIX shells. A child exit code outside 0 to 255, such as a Windows NTSTATUS crash code, exits 255.

The CLI never runs command substitutions from loaded files. To run them, use the library: Document::resolve_with_commands with a callback, or exec::Shell from the exec feature. To change the environment of the current process, use adotenv-process.