Ada environment and processes
last modified November 23, 2026
A program does not run in isolation. It inherits a table of environment
variables from the shell that started it, and it can start other programs of
its own. Ada covers the first with the standard package
Ada.Environment_Variables; process creation is not part of the
language standard, so the examples use GNAT.OS_Lib from the GNAT
run-time library. This article reads, sets, clears, and lists variables, starts
a child process, examines exit codes, and closes with a small command runner.
Reading a variable
Exists answers whether a name is present, and Value
returns its text. The two-argument form of Value takes a default
that is returned when the name is missing, which is usually more convenient
than testing first. The one-argument form raises Constraint_Error
for a name that is not defined.
with Ada.Text_IO;
with Ada.Environment_Variables;
use Ada.Text_IO;
procedure Env_Demo is
begin
Put_Line ("HOME set:" & Boolean'Image
(Ada.Environment_Variables.Exists ("HOME")));
Put_Line ("NO_SUCH_VAR_123 set:" & Boolean'Image
(Ada.Environment_Variables.Exists ("NO_SUCH_VAR_123")));
Put_Line ("fallback:" & Ada.Environment_Variables.Value
("NO_SUCH_VAR_123", "default"));
end Env_Demo;
HOME is set in almost every interactive shell, while the invented
name is not. The third line shows that the default is returned without an
exception, so a missing configuration value does not stop the program. Calling
the one-argument Value on the same name would instead raise
Constraint_Error, with a message that points into the run-time
library rather than into this source file.
$ gnatmake -gnat2022 -gnatwa env_demo.adb $ ./env_demo HOME set:TRUE NO_SUCH_VAR_123 set:FALSE fallback:default
Setting, reading and clearing
Set defines a variable or replaces its value in the environment
of the current process. The change is visible to Value
immediately and is also inherited by programs that this process starts later.
Clear removes the name again; it is not an error to clear a name
that is not present.
with Ada.Text_IO;
with Ada.Environment_Variables;
use Ada.Text_IO;
procedure Env_Set is
begin
Ada.Environment_Variables.Set ("DEMO_MODE", "on");
Put_Line ("DEMO_MODE =" & Ada.Environment_Variables.Value ("DEMO_MODE"));
Ada.Environment_Variables.Clear ("DEMO_MODE");
Put_Line ("after clear:" & Boolean'Image
(Ada.Environment_Variables.Exists ("DEMO_MODE")));
end Env_Set;
The first line prints the value that was just stored. After
Clear, Exists returns False, and a later
Value without a default would raise. These changes live only as
long as the process: the shell that started the program keeps its own
environment, and a child cannot modify the parent's variables.
$ gnatmake -gnat2022 -gnatwa env_set.adb $ ./env_set DEMO_MODE =on after clear:FALSE
Listing the environment
Iterate calls a procedure once for every variable, passing the
name and the value as strings. The order of the calls is not defined, so a
program that needs a particular order must collect the names and sort them.
The callback is passed with 'Access, and its parameters must match
the two String parameters of the access type. Here the attribute
applies to a subprogram rather than to an object: it produces an
access-to-subprogram value that matches the callback parameter of
Iterate.
with Ada.Text_IO;
with Ada.Environment_Variables;
use Ada.Text_IO;
procedure Env_List is
procedure Show (Name, Value : String) is
begin
if Name'Length >= 5
and then Name (Name'First .. Name'First + 4) = "DEMO_"
then
Put_Line (Name & "=" & Value);
end if;
end Show;
begin
Ada.Environment_Variables.Set ("DEMO_HOST", "localhost");
Ada.Environment_Variables.Set ("DEMO_PORT", "8080");
Ada.Environment_Variables.Iterate (Show'Access);
end Env_List;
The filter keeps the output short, because a real environment contains many
variables that belong to the shell and the desktop. The slice uses
Name'First instead of a hard-coded 1, and the and then
operator avoids taking the slice when the name is shorter than the prefix.
Only the two variables that the program set itself are printed.
$ gnatmake -gnat2022 -gnatwa env_list.adb $ ./env_list DEMO_HOST=localhost DEMO_PORT=8080
Starting a child process
GNAT.OS_Lib.Spawn runs a program and waits for it to finish. Its
second parameter is an Argument_List, an array of access values
that point at strings. Each argument is created with new String',
and the array aggregate uses the Ada 2022 bracket syntax. The child inherits
the current environment and writes to the same standard output and standard
error as the parent.
with Ada.Text_IO;
with GNAT.OS_Lib;
use Ada.Text_IO;
procedure Spawn_Demo is
Args : constant GNAT.OS_Lib.Argument_List :=
[1 => new String'("from"),
2 => new String'("Spawn")];
Status : Integer;
begin
Status := GNAT.OS_Lib.Spawn ("/bin/echo", Args);
Put_Line ("echo status =" & Integer'Image (Status));
end Spawn_Demo;
The line from echo appears before the status because
Spawn waits for the child to exit. No shell is involved unless the
program names one, so a pipe, a redirection, or a shell variable in an argument
would be passed literally to the child. The program is given as an absolute
path here; unlike a shell, Spawn does not search PATH
for a simple name, so a wrapper either receives a path or resolves one itself.
$ gnatmake -gnat2022 -gnatwa spawn_demo.adb $ ./spawn_demo from Spawn echo status = 0
Exit codes
The value returned by Spawn is the status that the operating
system reports for the child. Zero means success, and a non-zero value means
that the child failed or was interrupted. A shell can produce a chosen status
with sh -c "exit 3", which makes the meaning of the number easy to
see.
with Ada.Text_IO;
with GNAT.OS_Lib;
use Ada.Text_IO;
procedure Status_Demo is
Args : constant GNAT.OS_Lib.Argument_List :=
[1 => new String'("-c"),
2 => new String'("exit 3")];
Status : constant Integer := GNAT.OS_Lib.Spawn ("/bin/sh", Args);
begin
Put_Line ("status =" & Integer'Image (Status));
end Status_Demo;
The two arguments are -c and the command text, so the shell runs
exit 3 and stops with status 3. Spawn returns that
number unchanged. A program can pass it on to its own caller with
Ada.Command_Line.Set_Exit_Status, which is how a wrapper reports
the failure of the tool it ran.
$ gnatmake -gnat2022 -gnatwa status_demo.adb $ ./status_demo status = 3
An applied example: a command runner
The last program takes a program name and its arguments from its own command
line, builds the Argument_List for the child, runs it, and reports
the status. The number of child arguments is one less than
Argument_Count, so a call with only a program name produces a null
array, which Spawn accepts. A non-zero status is translated into
Failure for the caller of the runner.
with Ada.Text_IO;
with Ada.Command_Line;
with GNAT.OS_Lib;
use Ada.Text_IO;
procedure Run_Cmd is
N : constant Natural := Ada.Command_Line.Argument_Count;
begin
if N = 0 then
Put_Line (Standard_Error, "usage: run_cmd PROGRAM [ARG ...]");
Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
return;
end if;
declare
Args : GNAT.OS_Lib.Argument_List (1 .. N - 1);
Status : Integer;
begin
for I in 1 .. N - 1 loop
Args (I) := new String'(Ada.Command_Line.Argument (I + 1));
end loop;
Status := GNAT.OS_Lib.Spawn (Ada.Command_Line.Argument (1), Args);
Put_Line ("exit status =" & Integer'Image (Status));
if Status /= 0 then
Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
end if;
end;
end Run_Cmd;
The runner passes hello and there to
echo and reports success. It runs the shell for the second call,
so the shell itself exits with 3; the runner prints 3 and then returns 1 to its
own caller. The strings allocated with new are not freed before
the process ends, which is acceptable for a short tool but would matter in a
long-running program. The access types article
shows the deallocation step.
$ gnatmake -gnat2022 -gnatwa run_cmd.adb $ ./run_cmd /bin/echo hello there hello there exit status = 0 $ ./run_cmd /bin/sh -c "exit 3" exit status = 3 $ echo $? 1 $ ./run_cmd usage: run_cmd PROGRAM [ARG ...] $ echo $? 1
Next steps
The next article works with directories rather than individual variables. It creates and removes directories, splits a path into its parts, and searches a directory for files by pattern.
Source
In this article we have read environment variables with
Exists and Value, set and cleared them, listed them
with Iterate, started a child with GNAT.OS_Lib.Spawn,
examined its exit code, and built a command runner. The
command-line arguments article
supplies the arguments that the runner passes on.
Author
List all Ada tutorials.