$_ CLI In Depth
Learning progress 0%
case environment path dotfiles

System Deep-Dive: execve and the Environment Block

4 min read Module 9: Environment, PATH & Dotfiles Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:06

The call that starts a program

The system call execve takes three arguments: the path of the program to run, the argument vector, and the environment vector. The kernel reads the executable, replaces the memory of the calling process with the new program, and places the strings from the two vectors on the new stack. The process keeps its PID. It also keeps the working directory and every open file descriptor that does not have the close-on-exec flag set. Only the program image and the environment change.

For an ordinary command, bash does not call execve in its own process. It first calls fork, which creates a child that starts as a copy of the shell, and the child then calls execve. The parent waits for the child to finish and reads its exit status. The exec builtin skips the fork and replaces the shell itself, which is why exec does not return to the prompt.

What the kernel places in the new process

The kernel copies each environment string into the new process memory, in the order that bash passed them. The C library keeps a pointer to the array, usually called environ, and functions such as getenv search that array. This is why a program can read its environment with no special privilege and no connection to the shell. Calling setenv inside the program changes only the in-process copy, which vanishes when the program exits.

flowchart TB bash["bash forks a child"] --> child["Child copy of the shell"] child --> ex["execve with path, argv, and envp"] ex --> img["Kernel replaces the program image"] img --> arr["envp copied onto the new stack"] arr --> libc["C library reads environ"]

Watching the arrays with strace

strace prints system calls as they happen. With the -e trace=execve filter it shows only the program starts, and with -v it prints the full argument and environment arrays without shortening them. This is the most direct way to see which variables a child actually received, and it is more reliable than reading the shell configuration files.

BASH1 lines
strace -v -e trace=execve env -i A=1 env 2>&1 | tail -n 2

The traced call shows the environment array that env passed to the inner program. Here env -i cleared the inherited environment, so the only entry is the one you named. The trace output goes to standard error, which is why the command redirects it before the pipe.

A case: a service that cannot find a tool

Consider a job that works in a terminal but fails when a scheduler starts it. The error says that a tool named deploy-helper is not found. The first assumption is usually that the tool is missing, but the simpler explanation is that the job has a different PATH. A scheduler or a service manager usually starts the job with a small, fixed environment, and the terminal adds directories that the scheduler never sees.

The investigation follows the array. Start the job with the same clean environment you expect it to have, using env -i PATH=/usr/bin:/bin, and check whether the tool is found. Next, look at the environment block of the running process through /proc/PID/environ to see what the kernel actually received. Finally, compare the two lists. The difference is the missing directory, and the fix is to set PATH inside the job, not to copy the whole terminal environment into the scheduler.

Warning

Copying your full interactive environment into a service is a common fix that creates new problems. Secrets, a personal PATH, and a locale that the service does not need all come along. Pass only the variables that the program requires.

Limits and errors

The arrays have limits. A single string can be at most 128 KiB, and the combined arguments and environment must fit within the kernel limit for the stack. When the total is too large, execve fails with the error E2BIG, which bash reports as an argument list that is too long. A program that exports a very large value into every child can hit this limit without any obvious change, so keep large data in files rather than in variables.

Permission errors and missing files also come from execve. A missing interpreter in a script's first line produces a confusing error, because the kernel reports the script path even though the problem is the interpreter. Reading the system call trace resolves the question quickly, because each failed call shows the path it tried.

Shell variables, exports and exec

Visualizer

A variable stays private to the shell until it is exported. A child gets a copy of the exported set when it starts, and nothing a child changes comes back.

1 / 4
Shell memory
A1 (shell variable)
B2 (shell variable)
Environment block
B2 (exported)

export B copies B into the environment block. A stays only in the shell’s own memory. The environment block is what a program receives when it starts.

A=1; export B=2; bash -c 'echo "A=[$A] B=[$B]"'
Check yourself Question 1 of 4
Concept Check The call that starts a program recall

Which system call takes the program path, the argument array, and the environment array?

Recap & Key Takeaways

execve receives a path, an argument array, and an environment array, and the kernel copies them into the new process.
A child keeps its PID, working directory, and inherited file descriptors, but it gets a new program image and environment.
strace -v -e trace=execve shows the exact arrays a program received.
Pass only the variables a service needs instead of copying the whole interactive environment.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue