UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 45.6 Making Sure Your Script Runs with Bourne Shell, Without #! Chapter 45
Shell Programming for the Initiated
Next: 45.8 Handling Signals to Child Processes
 

45.7 The exec Command

The exec command will execute a command in place of the current shell; that is, it terminates the current shell and starts a new process (38.3) in its place.

Historically, exec was often used to execute the last command of a shell script. This would kill the shell slightly earlier; otherwise, the shell would wait until the last command was finished. This practice saved a process and some memory. (Aren't you glad you're using a modern system? This sort of conservation usually isn't necessary any longer unless your system limits the number of processes each user can have.)

exec can be used to replace one shell with another shell:

% exec ksh
$

without incurring the additional overhead of having an unused shell waiting for the new shell to finish.

exec also manipulates file descriptors (45.21, 45.22) in the Bourne shell. When you use exec to manage file descriptors, it does not replace the current process. For example, the following command makes the standard input of all commands come from the file formfile instead of the default place (usually, your terminal):

exec < formfile

- ML, JP


Previous: 45.6 Making Sure Your Script Runs with Bourne Shell, Without #! UNIX Power ToolsNext: 45.8 Handling Signals to Child Processes
45.6 Making Sure Your Script Runs with Bourne Shell, Without #! Book Index45.8 Handling Signals to Child Processes

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System