RunMat
GitHub

getenv — Read environment variables in MATLAB and RunMat.

getenv(name) returns the value of environment variable name visible to the current process. Calling getenv with no arguments returns a struct of available variables, consistent with MATLAB semantics.

Syntax

env = getenv()
value = getenv(NAME)

Inputs

NameTypeRequiredDefaultDescription
NAMEAnyYesVariable name query: char vector, string scalar/array, or cell array of char/string scalars.

Returns

NameTypeDescription
envAnyStructure containing all visible environment variables.
valueAnyResolved environment value(s) matching NAME input shape/type.

Errors

IdentifierWhenMessage
More than one input argument is supplied.getenv: too many input arguments
NAME input type is unsupported for getenv queries.getenv: NAME must be a character vector, string scalar, string array, or cell array of character vectors
Cell NAME entries are not character vectors or string scalars.getenv: cell array elements must be character vectors or string scalars
Internal conversion of environment query outputs fails.getenv: internal conversion failure

How getenv works

  • Accepts character vectors ('PATH') or string scalars ("PATH"). Character vector inputs return character vectors; string inputs return string scalars.
  • Vectorised calls are supported: pass a string array to receive a string array of values, or a cell array of character vectors to receive a cell array of character vectors.
  • When the requested variable is not defined, getenv returns an empty character vector or an empty string scalar (depending on the input type). This matches MATLAB’s “variable not found” semantics.
  • Calling getenv with no arguments returns a scalar struct. Field names are the environment variable names; field values are character vectors containing each variable’s value.
  • On Windows, environment variable lookups are case-insensitive, mirroring the operating system. On Unix-like systems, lookups are case-sensitive.
  • Trailing spaces in character matrix inputs are ignored so that padded rows created with MATLAB’s string manipulation functions remain compatible.
  • The builtin rejects non-text inputs (numeric arrays, logicals, GPU tensors, etc.) with a clear MATLAB-style diagnostic.

Does RunMat run getenv on the GPU?

getenv always runs on the CPU. If the argument originates from the GPU—for example, a string scalar produced by an accelerated builtin—RunMat gathers it to host memory before reading the environment. No GPU kernels are launched, and acceleration providers do not need to implement hooks for this builtin.

GPU memory and residency

No. getenv has no GPU implementation and simply queries the host environment. Passing GPU-resident values is supported (RunMat gathers them automatically), but there is no benefit to calling gpuArray yourself.

Examples

Read A Single Environment Variable

homeFolder = getenv("HOME");
disp(homeFolder)

Expected output:

homeFolder =
    "/"

Handle Missing Environment Variables

token = getenv("RUNMAT_TOKEN");
if token == ""
    warning("Token not configured.");
end

Expected output:

Warning: Token not configured.

Fetch Multiple Variables With A String Array

vars = getenv(["PATH", "SHELL"]);
disp(vars)

Expected output:

vars = 1x2 string
    "/usr/local/bin:/usr/bin:/bin"    "/bin/sh"

Use A Cell Array Of Character Vectors

cells = getenv({'HOME', 'PATH'});
disp(cells{1})

Expected output:

/

Inspect All Environment Variables

env = getenv();
disp(env.USER)

Expected output:

env =
  struct with fields:
    HOME: '/'
    PATH: '/usr/local/bin:/usr/bin:/bin'
    USER: 'user'

Cache The Environment For Repeated Access

env = getenv();
resultsFolder = fullfile(env.HOME, "results")

Expected output:

resultsFolder =
    "//results"

Using getenv with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how getenv changes the result.

Run a small getenv example, explain the result, then change one input and compare the output.

FAQ

What does getenv return when the variable is missing?

An empty character vector ('') for character inputs, or an empty string scalar ("") for string inputs. You can test for a missing variable with isempty(value) or value == "".

Does getenv support vectorised inputs?

Yes. Pass a string array or a cell array of character vectors to retrieve multiple values in one call. The output mirrors the input container type.

Are environment variable lookups case-sensitive?

RunMat follows the operating system. Lookups are case-insensitive on Windows and case-sensitive on Unix-like systems, matching MATLAB.

Does getenv modify the environment?

No. It is a read-only operation. Use the forthcoming setenv builtin to modify the environment once it is available.

Can I call getenv from GPU-accelerated code?

Yes. Inputs originating on the GPU are gathered automatically. The builtin still executes on the CPU, and the output lives on the host.

How do I access all environment variables at once?

Call getenv() with no arguments. The result is a struct whose fields contain character vectors for each environment variable.

What happens if an environment variable name is not a valid struct field?

RunMat preserves the exact name as a struct field. Access it via dynamic field references when the name contains characters that cannot be used in dot notation.

Are trailing spaces in character inputs significant?

No. Trailing spaces introduced by padding character matrices are stripped before the lookup so that MATLAB-style character arrays work as expected.

Does getenv include inherited environment variables?

Yes. The builtin reports every variable visible to the RunMat process, including inherited values.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how getenv is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.