exec

shell builtinLinux/Unix
The exec command is one of the most frequently used commands in Linux/Unix-like operating systems. exec Replace the current shell process with a specified command

Quick Reference

Command Name:

exec

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

exec [options] [arguments]

Common Use Cases

  • 1

    Process replacement

    Replace the current shell with another program without creating a new process

  • 2

    Resource conservation

    Avoid creating nested processes in scripts

  • 3

    File descriptor manipulation

    Permanently redirect I/O for the remainder of a script

  • 4

    Shell script chaining

    Transfer control from one script to another without returning

Syntax

exec [-cl] [-a name] [command [arguments...]]

Options

Option Description
-a name Pass name as the zeroth argument to command
-c Execute command with an empty environment
-l Place a dash in the zeroth argument to command (for login shells)

Examples

How to Use These Examples

The examples below show common ways to use the exec command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

# Basic Examples Basic
exec ls -la
Execute 'ls -la' and replace the current shell with it.
exec bash
Replace the current shell with a new bash shell.
# Advanced Examples Advanced
exec 3< input.txt Open a file descriptor for reading from input.txt. exec 4> output.txt Open a file descriptor for writing to output.txt. exec 2>&1 Redirect stderr to stdout. exec -a custom_name command Execute command with a custom process name.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

The exec command is a shell built-in that replaces the current shell process with the specified command, without creating a new process. When used without a command, it can be used to manipulate file descriptors. There are two primary uses for exec: 1. Process replacement: When exec is used with a command, it replaces the current shell with that command. The PID remains the same, but the program running is different. This means that once the command completes, the shell that was previously running does not return. 2. File descriptor manipulation: When exec is used with redirections but no command, it modifies the current shell's file descriptors. This allows for operations like opening files for reading or writing, or redirecting standard input/output/error. Exec is commonly used in shell scripts for: - Transitioning from a setup script to a main program without nesting processes - Permanently redirecting I/O for the remainder of a script - Managing file descriptors beyond the standard ones (stdin, stdout, stderr) When using exec to replace the current process, note that any commands in the script following the exec line will not be executed unless the exec command fails.

Tips & Tricks

1

When using exec with a command, remember that control will not return to the calling shell

2

Use exec for file descriptor operations like exec 3> file.txt to open a file for writing

3

Use exec 2>&1 to redirect stderr to stdout for the remainder of a script

4

Combine with redirection to permanently change I/O for the current shell: exec > logfile.txt

5

Use exec -a name command to set a custom process name

Common Use Cases

Process replacement

Replace the current shell with another program without creating a new process

Resource conservation

Avoid creating nested processes in scripts

File descriptor manipulation

Permanently redirect I/O for the remainder of a script

Shell script chaining

Transfer control from one script to another without returning

Daemon startup

Launch daemon processes with specific environment configurations

Related Commands

These commands are frequently used alongside exec or serve similar purposes:

Use Cases

1

Process replacement

Replace the current shell with another program without creating a new process

2

Resource conservation

Avoid creating nested processes in scripts

3

File descriptor manipulation

Permanently redirect I/O for the remainder of a script

4

Shell script chaining

Transfer control from one script to another without returning

5

Daemon startup

Launch daemon processes with specific environment configurations

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the exec command works in different scenarios.

$ exec
View All Commands