← Runtimes
Runtimes

Arguments, environment, and process startup

Know exactly what a program receives before main runs.

A new program starts with arguments, an environment and runtime initialization.

Outcome

Explain what a new process receives at startup (an argument list and a copied environment), predict where arguments and variables appear in each language, and start a child with exactly the arguments and environment it needs.

Terminology (6)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Argument vector (argv)
The list of strings a process receives when it starts. By convention the first names the program; the arguments follow.
Environment
A list of NAME=value strings a process receives when it starts. A child gets a copy of the environment it is given; later changes on either side stay in that process.
Inherit
Start a child with a copy of the parent’s current environment, which is what process-start APIs do when no environment is specified.
Unset versus empty
An unset variable has no entry; NAME= is an entry whose value is empty. Programs can tell them apart.
Shell
A program such as /bin/sh that parses a command line, splitting words and interpreting quotes and $, before starting a process with the resulting argument list.
process.argv / process.env
Node’s argument array (node path, script path, then arguments) and environment object.

What startup supplies

The starting process supplies an argument vector, an ordered list of strings, and an environment, a set of named string values. An argument can contain spaces or be empty. A shell parses command text to produce that list; a direct launch API accepts the elements themselves.

On Linux, an exec operation supplies those inputs to the kernel’s execve boundary. Exec replaces a process’s program image; the launch APIs used here arrange a separate child before that replacement. The child receives copies of the supplied arguments and environment; later changes stay in the process that makes them. Its runtime and initialization code then determine what happens before the application’s main work.

01What a process receives

Two lists of strings, copied at the start

A nightly report launcher starts one report process per job. The report titled “Q3 sales” arrives as “Q3”, and a region set for one job shows up in the next. What does a new process actually receive when it starts, and who decides it?

What a new process starts with
PartFormWho decides it
Argument vectorOrdered strings, including spaces and empty values; no embedded NUL on LinuxThe starting process, element by element
EnvironmentList of NAME=value stringsThe starting process: usually its own, sometimes edited
Program imageThe executable file, mapped into fresh memoryThe path the starter names

02The argument vector

Where your arguments start depends on the runtime

Start with the truncated title. Reduce the report child to an argument printer and give it show, two words and an empty string. This separates the launcher’s argument boundaries from the report formatter. The child finds show, counts the entries before it and prints the remaining list. Assume the child starts successfully.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { spawnSync } from 'node:child_process';

function main(): void {
  const index = process.argv.indexOf('show');
  if (index >= 0) {
    console.log(`entries before show: ${index}`);
    console.log(`arguments: ${JSON.stringify(process.argv.slice(index))}`);
    return;
  }

  spawnSync(process.execPath, [process.argv[1]!, 'show', 'two words', ''], {
    stdio: 'inherit'
  });
}

main();
terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc --noEmit --strict main.ts && node main.ts
The launcher starts this program again with three arguments: show, two words, and an empty string. What does the child print?

03The environment block

A child starts with a copy of an environment

With the title boundary fixed, check the region that leaks into the next job. Could a report child have changed its launcher’s environment? In this probe, the launcher sets MODE, starts a child and waits. The child prints and changes MODE; the launcher then prints its own value.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { spawnSync } from 'node:child_process';

function main(): void {
  if (process.argv[2] === 'child') {
    console.log(`child starts with MODE=${process.env.MODE}`);
    process.env.MODE = 'child';
    console.log(`child changed MODE=${process.env.MODE}`);
    return;
  }

  process.env.MODE = 'launcher';
  spawnSync(process.execPath, [process.argv[1]!, 'child'], { stdio: 'inherit' });
  console.log(`launcher still has MODE=${process.env.MODE}`);
}

main();
The launcher sets MODE=launcher in its own environment, starts the child and waits. The child prints MODE, sets it to child and prints again. What does the launcher print afterwards?

04Before main

What runs before your main

The launcher may also change normal environment variables between jobs, so a reusable launch template needs a clear snapshot lifetime. The report loads settings too. If that loading happens in a global initializer, would setting configuration at the beginning of main be early enough? The kernel and loader prepare the new program image and initial stack before its runtime reaches the application. The runtime then follows its language’s startup rules. This reduced probe prints when loading happens; it does not itself read configuration. Node loads a script whose main function is an ordinary function call.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const settings = loadSettings();

function loadSettings(): string {
  console.log('loading settings');
  return 'ready';
}

function main(): void {
  console.log(`main starts with settings=${settings}`);
}

main();
Which line does this program print first?

05Practice

Start each report with what it needs

The launcher’s remaining requirement is to keep its dispatch token for its own work while withholding it from report children. Normal variables may change between jobs; each report needs the current values, not a template saved for an earlier job. The lab continues the title and region repairs, then tests that boundary across multiple jobs and similarly named variables. Its report child prints what it actually received; these are real child processes, not the process-table model from the preceding lesson. Stdin uses job <title>|<region> and env NAME=value lines; the first separator divides each pair, and a missing or empty region means unset in the child.

The nightly report launcher

One launcher, real child processes. Pass each job’s title as one argument, give each report its own region, then keep a secret out of every report without losing it in the launcher.

  1. easy · R-START-V4-1 Pass the title
  2. medium · R-START-V4-2 Set the region
  3. hard · R-START-V4-3 Keep the token out
Check this idea later
A backup script runs a tool by building the string "archive " + folder and passing it to a shell. The folder is named Client Files. What does the tool receive?
A service starts a worker, then updates LOG_LEVEL in its own environment. What LOG_LEVEL does the running worker have?
A test harness starts a program with an environment option that contains only DEBUG=1, using an API where a supplied environment is complete. What happens to PATH in the child?
Sources
In the frontend Build-time public settings are earlier snapshots. Optional

A report-launcher dashboard shows a public region label. Changing the container’s startup environment can configure its server process correctly while leaving that label unchanged: the browser code may already contain a value captured during the frontend build. Check which process read the variable and when.

app/region-label.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
'use client';

export default function RegionLabel() {
  return <p>{process.env.NEXT_PUBLIC_REGION}</p>;
}

Next.js inlines a direct NEXT_PUBLIC_REGION reference into browser code at build time. Promoting the same built artifact to a container with another startup value does not replace that embedded label. Values required at runtime need a server-to-client data path designed for that purpose. Public variables are visible to browser users.

Sources: Next.js: environment variables,SvelteKit: explicit environment variables.

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.