The POSIX 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 process 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 previous shell was a login shell, your session terminates.
If exec is called from a shell script, the script terminates. Commands that follow an exec call in the script will never be executed.It can be used to redirect the standard input or the standard output 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.
Syntax
Format 1: |
exec program[ redirection] |
Format 2: |
exec redirection |
Replacing the shell with another program
Any command, program or shell script, but not another sh command. 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.
If the specified program reads from standard input or writes to standard output, a file can be assigned for the input or output instead.
Redirecting the shell's standard input/standard output
Commands that read from standard input or write to standard output are assigned a file for their input/output. The redirection applies to all commands that the current shell executes after exec.
|
Locale
The following environment variables affect the execution of exec: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example 1
What happens when you make the following input?
First, the exec command terminates the shell and then causes the date command to be executed. The current date is displayed on the screen. If you have entered the command in your POSIX shell the BS2000 prompt is output. If you want to recommence work with the POSIX shell you must re-enter the START-POSIX-SHELL command. 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:
Since proc3 is called from proc2 with exec, both shell scripts run under the same process ID. |
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:
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. |
See also
exec() [4] |