NAME
exec — execute
commands and open, close, and/or copy file
SYNOPSIS
descriptors |
DESCRIPTION
exec [command [argument . . . ]] The
exec utility opens, closes, and/or copies file
descriptors as specified by any redirections as part of the command. If
exec is specified without command or arguments, and
any file descriptors with numbers > 2 are opened with associated
redirection statements, it is unspecified whether those file descriptors
remain open when the shell invokes another utility. If
exec is specified with command, it shall replace the
shell with command without creating a new process. If arguments are
specified, they are arguments to command. Redirection shall affect the
current shell execution environment.
EXIT STATUS
If command is specified, exec shall not
return to the shell; rather, the exit status of the process shall be the
exit status of the program implementing command, which overlaid the shell.
If command is not found, the exit status shall be 127. If command is found,
but it is not an executable utility, the exit status shall be 126. If a
redirection error occurs (see 3.8.1), the shell shall exit with a value in
the range 1−125. Otherwise, exec shall return
a zero exit status.
RATIONALE
Most historical implementations are not conformant in that foo=bar
exec cmd does not pass foo to cmd. Earlier drafts
stated that ‘‘If specified without command or argument, the
shell sets to close-on- exec file numbers greater
than 2 that are opened in this way, so that they will be closed when the
shell invokes another program.’’ This was based on the
behavior of one version of the KornShell and was made unspecified when it
was realized that some existing scripts relied on the more generally
historical behavior (leaving all file descriptors open). Furthermore, since
the application should have no cognizance of whether a new shell is simply
fork()
ed, rather than
exec ed, it could not consistently rely on
the automatic closing behavior anyway. Scripts concerned that child shells
could misuse open file descriptors can always close them explicitly, as
shown in one of the following examples. Examples: Open readfile as file
descriptor 3 for reading: exec 3< readfile Open
writefile as file descriptor 4 for writing: exec
4> writefile Make unit 5 a copy of unit 0: exec
5<&0 Close file unit 3: exec
3<&− Cat the file maggie by replacing the current shell with
the cat utility: exec cat maggie