Table of Contents

Bash - Exec (No New Process) - builtin command

About

If command is specified, it replaces the shell. No subshell (ie new process) is created.

The shell:

Example

Without command: for redirection management

You can use exec when you want to capture all stdout of a series of command.

When a command is not specified:

Example:

# Save the original stdout to the file descriptor (3)
exec 3>&1

# Redirect stdout to the file output.txt
exec 1> | /dev/shm/output.txt

# Command that output to stdout
# ............

# Restore original stdout file descriptor
exec 1>&3

# Close file descriptor 3
exec 3>&-

# Now you can read the captured output from the `output.txt` file descriptor
cat /dev/shm/output.txt

Syntax

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

where:

Return value

If command cannot be executed for some reason,

Without command, redirections takes effect

If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1.

Documentation / Reference