Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Processes and Signals

Linux exec command

Reference for the Bash exec builtin: replace the current shell with a program, open and close file descriptors for the whole script, and use -a, -c and -l.

exec is a shell builtin with two separate jobs. With a command, it replaces the running shell with that program: no new process is created, the program takes over the shell's process ID, and the shell never returns. Without a command, it only applies its redirections to the current shell, which is the standard way to open files for a whole script.

Because the shell disappears, exec is useful at the end of wrapper scripts (the wrapped program becomes the real process, so signals from the init system reach it directly) and for permanent redirection of standard streams.

Terminal
exec [-cl] [-a NAME] [COMMAND [ARGUMENT]...] [REDIRECTION...]

Try it

Bash Replacing the shell

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Redirections that last for the whole script

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
exec [-cl] [-a name] [command [arguments]] [redirections]
exec redirections
Name Description
command Program to run in place of the shell. Looked up through PATH like any command, but functions, aliases and builtins are not used: it must be an external program.
arguments Passed to the program as its arguments.
-a name Pass name as the program's argv[0] (the name it believes it was started with).
-c Run the program with an empty environment.
-l Put a dash in front of argv[0], the way login starts a login shell.
redirections Without a command, applied to the current shell and left in place.

Returns: with a command, nothing: the shell is gone. If the program cannot be found the status is 127; if it is found but cannot be run it is 126. Without a command the status is 0, or 1 if a redirection fails.

Errors and edge cases: a non-interactive shell exits when exec fails; an interactive one stays alive and prints an error (unless shopt -s execfail is set for non-interactive shells, which keeps them running after a failed exec).

Usage notes

  • No return. Code after a successful exec COMMAND is dead. That makes exec the natural last line of a launcher script: exec "$real_binary" "$@" leaves one process instead of two.
  • Same PID. Because the process is replaced, $$ keeps its value and any trap set in the shell disappears with it; the new program starts fresh with default signal handling, apart from ignored signals, which stay ignored.
  • Containers and services. A shell used as a container entry point swallows signals unless it ends in exec. With exec, the application is PID 1 and receives SIGTERM directly.
  • Redirection-only form. exec 3>file opens a descriptor for the whole script; exec 3>&- closes it. Descriptors above 9 are possible with exec {fd}>file, which lets Bash pick a free number and store it in fd.
  • Redirecting standard streams with exec > file is permanent for the rest of the shell. To restore them, first save the originals: exec 5>&1, later exec 1>&5 5>&-.
  • A subshell contains it. ( exec > log; ... ) limits the redirection to the parentheses.
  • Not for builtins or functions. exec cd /tmp fails because cd is a builtin; exec looks only for programs.
  • Do not confuse with xargs or -exec. find -exec and xargs start child processes; exec replaces the current one.