Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

exec - execute commands and open, close or copy file descriptors

&pagelevel(4)&pagelevel

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
Format 1: exec program[ redirection]


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.

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.


redirection can be specified as follows:


>fileThe standard output of the specified program is redirected to file. Any data previously in file will be deleted.
>>fileThe standard output of the program specified is redirected to file. Data already contained in file is not deleted; program output is appended to it instead.
2>fileThe standard error output of the specified program is redirected to file.
<fileThe standard input of the specified program is redirected to file, i.e. the program reads its input from this file.


Redirecting the shell's standard input/standard output
Format 2: exec redirection


redirection

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.


redirection can be specified as follows:


>file

All commands executed by the current shell write their output to the specified file sequentially. Data previously 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 cancelled by:

  • pressing CTRL+D or @@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 cancelled within the script by including the command exec >/dev/tty at the appropriate position in the script. This will redirect the standard output of all subsequent commands back to the screen.

>>fileAll 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>fileStandard 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 following 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 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?

$ 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.

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:

$ 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.

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.

See also

exec() [4]