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 dictionary of string names and values.
Syntax
env = getenv()
value = getenv(NAME)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
NAME | Any | Yes | — | Variable name query: char vector, string scalar/array, or cell array of char/string scalars. |
Returns
| Name | Type | Description |
|---|---|---|
env | Any | Structure containing all visible environment variables. |
value | Any | Resolved environment value(s) matching NAME input shape/type. |
Errors
| Identifier | When | Message |
|---|---|---|
| — | 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"). Both scalar forms return character vectors. - 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 a scalar requested variable is not defined,
getenvreturns an empty character vector. Nonscalar string and cell containers retain their documented container type and dimensions. - Calling
getenvwith no arguments returns a dictionary whose keys and values are strings. - On Windows, environment variable lookups are case-insensitive, mirroring the operating system. On Unix-like systems, lookups are case-sensitive.
- RunMat mode additionally accepts padded multirow character matrices and cells containing string scalars; both conveniences are independently disabled in MATLAB-compatible modes.
- The builtin rejects non-text inputs, including every integer class and resident numeric handles, before provider access or environment lookup.
Does RunMat run getenv on the GPU?
getenv always runs on the CPU. No GPU kernels, downloads, or acceleration-provider hooks participate.
GPU memory and residency
No. getenv is a host environment query with textual inputs. Resident numeric values are invalid names and reject before provider access.
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.");
endExpected 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:
"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?⌄
A scalar character-vector or string-scalar query returns an empty character vector (''). You can test for a missing variable with isempty(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?⌄
The environment lookup always executes on the host and accepts textual names. Resident numeric values are not converted into names and reject before provider access.
How do I access all environment variables at once?⌄
Call getenv() with no arguments. The result is a dictionary with string keys and values.
What happens if an environment variable name contains punctuation?⌄
The no-argument dictionary preserves the exact string key, so names do not need to be valid structure field identifiers.
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.
Related Io functions
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fileattrib · fileparts · fullfile · genpath · getpref · isenv · isfile · isfolder · ispref · ls · matlabroot · memmapfile · mkdir · movefile · open · opentoline · path · pathsep · pcode · pwd · readstruct · rehash · restoredefaultpath · rmdir · rmpath · run · savepath · setenv · setpref · system · tempdir · tempname · uigetdir · uigetfile · uiputfile · unsetenv · userpath · what · winqueryreg · xmlread · xmlwrite
Tabular
arrayDatastore · csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · fileDatastore · parquetDatastore · parquetinfo · parquetread · readcell · readmatrix · readtable · readtimetable · spreadsheetImportOptions · writecell · writematrix · writetable · writetimetable · xlsread · xlswrite
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite · readlines · writelines
Import
Json
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how getenv is executed, line by line, in Rust.
- View the source for getenv in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.