exec Command in Linux (original) (raw)

Last Updated : 7 Mar, 2026

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.

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 bash

**Output:

A new Bash prompt appears. It may look similar to the previous one.

**Note:

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 ls

**Output:

file1.txt
script.sh
Documents
Downloads

**Notes:

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] [arguments]
exec [options] [arguments] [redirection]
exec [redirection]

**Note:

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

**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 env

**Output:

**Notes:

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

**Example: Run sleep with a Custom Process Name

**Command:

exec -a myprocess sleep 60

**Expected Behavior:

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

**Example: Start a Login Shell with bash

**Command:

exec -l bash

**Expected Behavior:

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

**Output:

file1.txt
script.sh
Documents
Downloads

**Notes:

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

**Output (Terminal):

Nothing appears on the terminal.

**Output (Inside output.txt):

Hello World
file1.txt
script.sh
Documents
Downloads