The exec command in Linux is a shell built-in used to replace the current shell with another command. Unlike normal commands that start a new process, exec does not create a new process. Instead, it runs the given command in place of the current shell.
- Run a program without creating an extra process
- Replace the current terminal session with another program
- Change input/output behavior of the current shell
- Improve script efficiency by avoiding unnecessary process creation
Example 1: Replace Current Shell with bash
exec replaces the current shell process with a new bash process. Instead of starting a child shell, it completely transforms the existing shell into a new Bash instance. This means the original shell no longer exists.
Command:
exec bashOutput:
A new Bash prompt appears. It may look similar to the previous one.
Note:
- When you type exit, the terminal session closes.
- If you run bash without exec, exiting returns to the previous shell.
- With exec bash, there is no previous shell to return to.
Example 2: Replace Shell with ls
Here, exec replaces the shell with the ls command. Since ls runs quickly and exits, the shell process ends immediately after displaying the output.
Command:
exec lsOutput:
file1.txt
script.sh
Documents
Downloads
- After displaying the directory contents, the terminal session closes.
Notes:
- This happens because the shell was replaced by ls.
- Once ls finishes, there is no shell process left running.
Syntax
The exec command has a flexible syntax depending on whether you are replacing the shell with a command or modifying file descriptors.
exec [options] <command> [arguments]
exec [options] <command> [arguments] [redirection]
exec [redirection]
- exec: Invokes the shell built-in command that replaces the current shell process or modifies file descriptors.
- [options]: Optional flags that modify the behavior of exec (e.g., -c, -l, -a name).
- <command>: The program or command to execute. If provided, the current shell is replaced by this command.
- [arguments]: Optional parameters passed to the specified command.
- [redirection]: Used to modify input/output behavior (e.g., >, <, 2>, >>).
Note:
- If no command is given, redirection modifies the current shell itself.
- If <command> is provided: the shell is replaced.
- If only redirection is provided: the shell continues, but its file descriptors are modified.
- exec does not create a new process.
Options of exec Command
1. -c
The -c option runs the command with an empty environment. This means all existing environment variables (like PATH, HOME, etc.) are cleared before executing the command.
Syntax:
exec -c <command>Example: Run Command Without Environment Variables
When you normally run env, it shows all environment variables. Using exec -c, the command runs with no environment variables.
Command:
exec -c envOutput:
<no output>Notes:
- Since the environment is cleared, commands depending on PATH may fail.
- You may need to specify the full path of commands, such as /bin/ls.
- After execution, the shell is replaced by the command.
2. -a name
The -a option allows you to pass a custom name as the zeroth argument of the command. This changes how the process appears in tools like ps without affecting the actual command being executed. The shell is replaced by the command, but the displayed process name will be the one you specify.
Syntax:
exec -a <name> <command>Example: Run sleep with a Custom Process Name
Command:
exec -a myprocess sleep 60Expected Behavior:
- The current shell is replaced by the sleep command.
- The process runs for 60 seconds.
- Using ps -ef | grep sleep from another terminal may show the process name as myprocess.
3. -l
The -l option passes a dash (-) as the zeroth argument to the command, making it behave like a login shell. This can affect how certain shell startup files are executed, such as .bash_profile or .profile. The current shell is replaced by the command, but it starts in login mode.
Syntax:
exec -l <command>Example: Start a Login Shell with bash
Command:
exec -l bashExpected Behavior:
- The current shell is replaced by a new Bash shell.
- Bash behaves like a login shell, reading startup files like .bash_profile.
- After exiting, the terminal session closes since the original shell was replaced.
Key Behaviors of exec Command
The exec command in Linux works in two main modes, depending on whether you provide a command or not. Understanding these modes is essential for using exec effectively in scripts and shell sessions.
1. Exec with a Command
When a command is provided, exec replaces the current shell with the specified command. No new process is created, and the original shell is terminated. Any arguments provided are passed to the command, and any redirections are applied.
Example:
exec ls -l- ls -l runs in place of the current shell.
- Once ls finishes, the shell is gone.
- Redirections (like > file.txt) can also be applied.
Output:
file1.txt
script.sh
Documents
Downloads
Notes:
- If the command is not found, both exec and the shell exit with an error.
- Use the full path if the command depends on environment variables (like PATH).
2. Exec without a Command
If no command is provided, exec does not replace the shell. Instead, it can modify the current shell’s file descriptors such as standard input, output, or error. This is useful for redirecting input/output of all subsequent commands.
Example:
exec > output.txt
echo "Hello World"
ls
- The first exec redirects standard output to output.txt.
- The echo and ls commands write their output to the file instead of the terminal.
- The shell continues to run, unlike the first mode.
Output (Terminal):
Nothing appears on the terminal.
Output (Inside output.txt):
Hello World
file1.txt
script.sh
Documents
Downloads