Previous Table of Contents Next

Module 45
exit (csh, ksh, sh) / logout (csh)


The internal exit command causes the current shell to terminate. If the current shell is a child process, execution returns to the parent process when it exits. If the current shell is the login shell, you're logged off the system when the shell terminates.

The Ctrl-D character can be used to perform the same function. The Ctrl-D informs the shell it has reached the end-of-file (EOF). Because the shell is a filter and filters terminate when EOF is reached, the shell terminates and you log off the system or return to the parent process.

If you are writing a shell script and you need to provide a return code (exit status) to a parent process, you can use the exit n format, where n is the return code you want returned to the calling (parent) program.


Following is the general format of the exit and logout commands.

     exit [ n ]

C Shell
exit [n]


Only one option may be used to control how exit functions.

n The return code (exit status) you wish to return from the termination of the current shell


To enter a subshell you have to run the ksh, csh, or sh program. For example, you would type

     cj> ksh

and press Return. To return to your parent shell you type

     cj> exit

and press Return. The exit and Ctrl-D are handled a little differently by the shell. An exit command is processed as a normal internal command. Whereas the Ctrl-D is interpreted as EOF (end-of-file) and causes the shell to exit immediately. By using exit you can specify a return code value. This value is stored in the parent shell variable $? ($status-csh). An example of how you might use this return code and variable will help explain return codes.

Suppose you want to write a script named readable that returns a 0 if a file is readable and a 1 if the file is not readable.

     if [ -r "$1" ]
     then exit 0
     else exit 1

You pass readable a single argument, which is a filename. If the file is a readable file, the return code is a 0; otherwise, a 1 is returned. Now in your parent shell you can use the $? ($status-csh) variable to test whether the file is readable are not. For example, your parent shell might look like this:

     readable somefile
     if [ $? ]
     then echo "Processing the file"
     else echo "Cannot read the file"

This provides you with the capability to tell whether a child shell script completed with or without errors.

C Shell
The logout command is an internal csh command that logs you off the system. It only works while in the login shell. If you are in a subshell, the logout command will not perform an exit or logout.
When you type logout and press Return in your login shell, the shell executes the commands in the ~/.logout file before exiting.

The logout command can be duplicated in the ksh by setting an alias that performs an exit for you. Some systems implement logout in the shell, whether it be sh or ksh. It is not implemented on all UNIX systems; therefore, you may want to create it as one of your own aliases. You can make the logout alias perform other functions before logging you out of a shell or off of the system. Refer to Module 4 for more details on alias. The following example is a possible logout alias:

     cj> alias logout="clear; du -s $HOME; times; exit"
     cj> logout

In this example your screen is cleared, your disk space usage is displayed, and the shell status information is displayed before you are logged off the system.

Another alternative is to set a trap in your ~/.profile to execute the ~/.logout file. Simply place the following line in your ~/.profile.

     trap '. $HOME/.logout; exit 99' 0

When you log out of the system with the exit command, the shell will trap signal 0 and execute the $HOME/.logout file and then exit the shell.

Logging out of the system is important. By logging out you prevent unfriendly people from causing possible mischief, using your terminal and user ID. If you are away from your terminal for more than a few (5) minutes, log off! If your terminal resides in a secure area, this may not be that important, but it's better to form the habit early than be sorry later.

Logging out provides someone else with a free port (/dev/ttyXX) to use if you are dialing in on a modem. If you are hardwired, this may not make any difference. Some UNIX systems are configured to log you out if you turn your terminal off. Terminals connected via modems usually perform in this manner. Check with your system administrator before you turn your terminal off to log out when using a modem. If the system does not recognize that you dropped the carrier on the modem connect, you will not be logged out. Therefore, the next person that dials in on that system modem will receive your shell prompt instead of the system's login prompt.


Refer to Module 71 describing ksh.


The exit and logout commands do not display output unless you have them aliased to perform some pre-exit commands.


The exit and logout commands and the Ctrl-D character provide a means of exiting or logging off of the system when you are ready to stop using it. Any of these three can be used to exit subshells and return to the previous shell and environment. The exit command is also used to exit a shell script without completing the following commands.


In this activity you use the ksh and exit command to create a subshell and then exit back to your login shell. Begin at the shell prompt.

1.  Type ksh and press Return. Notice a new prompt is displayed on your terminal screen. You are now in a subshell.

C Shell
Type csh and press Return.

2.  To return to your login shell, type exit and press Return. You will see your login shell prompt appear.
3.  Turn to Module 92 (SV), Module 100 (BSD) to continue the learning sequence.

Previous Table of Contents Next