exec(1) exec(1)
NAME
exec - overlay current shell (execute)
SYNOPSIS
exec program [redirection] Format 1
exec redirection Format 2
DESCRIPTION
The shell built-in exec performs two functions:
- It overlays the current shell with another program (Format 1). When
this program starts, the current shell is terminated. No new pro-
cess is created, as is evident from the fact that the process ID
does not change (see Example 2).
If you enter exec interactively, when the specified program exits
you return to the parent shell of the previous shell; if your pre-
vious shell was a login shell, your session terminates.
If exec is called from a shell script, the script terminates. Com-
mands that follow an exec call in the script will never be exe-
cuted.
- It can be used to redirect the standard input or the standard out-
put of the shell to a file (Format 2). All commands entered after
exec has been executed will read from or write to this file until
you terminate the current shell.
The Korn shell ksh has additional redirection options [see ksh(1)].
Format 1: Replacing the shell with another program
exec program [redirection]
program
Any command, program or shell script, but not another shell
built-in. You will need execute permission for the associated
file.
The current shell terminates, and program is executed instead. On
completion of the specified program, control reverts to the old
shell's parent, or the welcome screen is displayed of the old
shell was a login shell.
redirection
If the specified program reads from standard input or writes to
standard output, a file can be assigned for the input or output
instead.
Page 1 Reliant UNIX 5.44 Printed 11/98
exec(1) exec(1)
redirection can be specified as follows:
>file The standard output of the specified program is redirec-
ted to file. Any data previously in file will be deleted.
>>file The standard output of the program specified is redirec-
ted to file. Data already contained in file is not
deleted; program output is appended to it instead.
2>file The standard error output of the specified program is
redirected to file.
<file The standard input of the specified program is redirected
to file, i.e. the program reads its input from this file.
Format 2: Redirecting the shell's standard input/standard output
exec redirection
redirection
Commands that read from standard input or write to standard out-
put are assigned a file for their input/output. The redirection
applies to all commands that the current shell executes after
exec.
redirection can be specified as follows:
>file All commands executed by the current shell write their
output to the specified file sequentially. Data previ-
ously in file is deleted.
The output from all commands can be collected in this
way.
If you have entered exec interactively, the redirection
can only be canceled by:
- pressing CTRL-D. This terminates the current shell.
- entering exec >/dev/tty. This redirects the standard
output back to the screen.
If exec is called from a shell script, the redirection
will only apply to commands that follow the exec call in
the script. Redirection can be canceled within the script
by including the command exec >/dev/tty at the appropri-
ate position in the script. This will redirect the stan-
dard output of all subsequent commands back to the
screen.
Page 2 Reliant UNIX 5.44 Printed 11/98
exec(1) exec(1)
>>file All commands executed by the current shell write their
output to the specified file sequentially. Data already
contained in file is not deleted; program output is
appended to it instead.
2>file Standard error is redirected to file for all commands
executed by the current shell.
<file If you specify exec interactively, the current shell will
read the commands to be executed from the specified file.
The shell exits after the last command.
If this command appears in a shell script, all commands
that follow the exec call in the script read their input
from the specified file. Each read operation modifies the
position of the read pointer in this file. If the read
pointer is set to the end of the file (EOF), all follow-
ing commands will receive no input.
The command exec </dev/tty can be used to redirect the
standard input back to the keyboard for all subsequent
commands.
file If you have entered exec interactively, file must
be the name of a shell script. You will only need
read permission for this shell script.
When exec is called from a shell script, file
designates the name of the file from which all
subsequent commands are to obtain their input.
LOCALE
The LCMESSAGES environment variable governs the language in which
message texts are displayed. If LCMESSAGES is undefined or is defined
as the null string, it defaults to the value of LANG. If LANG is like-
wise undefined or null, the system acts as if it were not internation-
alized.
The LCALL environment variable governs the entire locale. LCALL
takes precedence over all the other environment variables which affect
internationalization.
EXAMPLES
Example 1
What happens when you make the following input?
$ exec date
First, the exec command terminates the shell and then causes the date
command to be executed. The current date is displayed on the screen.
Page 3 Reliant UNIX 5.44 Printed 11/98
exec(1) exec(1)
If you entered the exec command from your login shell, the welcome
screen will then be displayed. You will now need to log into the sys-
tem again.
If you entered the command from a subshell, control will be returned
to the parent shell.
Example 2
As indicated earlier, the process ID does not change when the current
shell is replaced by another program. This concept is demonstrated
with reference to the following files:
- Contents of the file proc1:
echo The process ID of proc1 is: $$
sh proc2
- Contents of the file proc2:
echo The process ID of proc2 is: $$
exec proc3
- The file proc3, which must be executable, contains the following:
echo The process ID of proc3 is: $$
The shell script proc1 is now initiated:
$ sh proc1
The process ID of proc1 is: 2755
The process ID of proc2 is: 2760
The process ID of proc3 is: 2760
Since proc3 is called from proc2 with exec, both shell scripts run
under the same process ID. To be exact, the shell that executes proc2
is replaced by the shell that executes proc3.
Page 4 Reliant UNIX 5.44 Printed 11/98
exec(1) exec(1)
Example 3
The exec command is used in the exctest shell script to redirect the
standard input to the file /etc/group for all following commands. The
contents of the exctest file is given below:
: Invocation with sh exctest
exec </etc/group
read lines
echo $lines
echo
cat
The shell script exctest is now initiated:
$ sh exctest
root::0:root
daemon::1:daemon
sys::2:sys:
bin::3:bin,admin
uucp::4:
.
.
.
The shell built-in read assigns the first line of the /etc/group file
to the variable line. The echo $line command outputs the content of
this variable.
The cat command also reads its input from the /etc/group file. Since
the read pointer is now set to the second line of this file, cat
displays the file's contents from the second line onward.
The read pointer is now set to EOF. Any further commands after the cat
call would consequently receive the null string as input.
NOTES
Some differences in behavior may occur when using exec, depending on
which shell is being used. The possible differences are not described
specifically.
SEE ALSO
ksh(1), sh(1), exec(2).
Page 5 Reliant UNIX 5.44 Printed 11/98