exec is the family of system calls that replaces the current process's program with a new one. Unlike fork, which creates a new process, exec transforms the existing one: same PID, same file descriptors, same parent, but a new code and data segment loaded from the specified executable. Exec never returns on success, because the calling code no longer exists.
The standard library offers several flavours (execve, execl, execlp, execvp, etc.) differing in how they accept arguments and whether they search $PATH. All eventually call the execve(2) system call.
In shell, exec without a command manipulates file descriptors without starting a new process; with a command, it replaces the current shell:
exec 3< file.txt # open fd 3 on file.txt
exec > output.log # redirect stdout for the rest of the script
exec env # replace shell with env (no return)
The fork-then-exec idiom is the heart of Unix process creation: fork duplicates, exec replaces. This two-step design lets the child inherit the parent's environment and manipulate descriptors (for pipes, redirections, environment tweaks) between fork and exec, which is far more flexible than a single "spawn" call.
Discussed in:
- Chapter 10: Processes and Job Control — Fork and Exec
Also defined in: Textbook of Linux