![]() |
VOOZH | about |
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.
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.
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
Notes:
- This happens because the shell was replaced by ls.
- Once ls finishes, there is no shell process left running.
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]
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.
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.
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 -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 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.
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 -lOutput:
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).
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
Output (Terminal):
Nothing appears on the terminal.
Output (Inside output.txt):
Hello World
file1.txt
script.sh
Documents
Downloads