What Exec Does in Linux
The exec builtin in Linux replaces the current shell process with a new command instead of spawning a child process. When you run exec followed by a command, the shell stops existing and the specified program takes over the same process ID. This behavior is fundamental to process management in Unix-like systems and affects how scripts and interactive sessions behave.
More from this site
Keep reading the latest coverage
Why Use Exec in a Shell Script
Scripts use exec for three primary reasons. First, it replaces the shell process with another program, so no extra process remains after the command finishes. Second, it can redirect file descriptors for the remainder of the script. Third, it can launch a different program in the same process slot, which is useful for initialization routines or containment. Example:
- exec python3 app.py — replaces the shell with the Python interpreter.
- exec > out.log — redirects all subsequent output of the current shell to a file.
How Exec Differs from Running a Command Normally
When you type a command in a shell without exec, the shell forks a child process, the child runs the command, and the parent shell waits and then resumes. With exec, there is no fork. The shell itself becomes the command, so the PID stays the same and, when the command exits, the shell session ends unless exec is used in a subshell or with redirection only.
Common Patterns and Pitfalls
Using exec inside a loop or a conditional can end a script unexpectedly if the command fails or returns control. Because exec replaces the process, there is no return to the calling shell context. Wrapping exec in a subshell with parentheses preserves the parent shell: (exec somecmd). Another common pattern is exec with file redirection to avoid repeatedly opening and closing file descriptors.
Exec with File Descriptors
You can combine exec with redirections to change how a script communicates with files and pipes. The syntax exec 3< input.txt opens file descriptor 3 for reading, and exec 4>> log.txt opens descriptor 4 for appending. These persist for the rest of the script and let long-running scripts manage I/O more cleanly than repeated redirection operators.
Exec in Docker and Process Containment
In container environments, exec is often used to run a command inside a running container or to set the initial process of an image. When the exec-ed command is PID 1 inside a container, it receives signals directly, so proper signal handling in that command becomes critical for graceful shutdowns.
Comparison: exec vs fork and System Calls
| Mechanism | Process ID | Shell Survives | Typical Use |
|---|---|---|---|
| Normal command | New child PID | Yes | Routine command execution |
| exec builtin | Same PID | No (unless in subshell) | Replace shell, redirect descriptors |
| fork + exec | New child PID | Yes | Low-level process creation in C |
When You Should Avoid exec
Avoid exec when you need the shell to continue running after the command, when the command might fail and you want error handling afterward, or when the command is interactive and you expect to return to the prompt. If you forget that exec replaces the process, scripts can terminate silently or leave file descriptors in unexpected states.