Previous Table of Contents Next


Return codes are two bytes of status information returned to the shell after each command terminates. The first byte is returned by the system. It provides the cause for termination. If normal system termination occurred, a zero is returned. The second byte is returned by the program or command. Upon successful termination the default return code is zero (0). If the program detects problems and terminates, then a nonzero return code is placed in the second byte. Return codes may be referred to as "exit code," "exit status," "return code," or "return status."

The return code is assumed to be zero if the program terminated under normal conditions. Therefore, some of the modules in this book do not have RETURN CODE sections, because the return codes are zero and nonzero with no special meaning besides normal and abnormal termination.

The shell assigns the return code to the ? variable. You can refer to the variable by using ${?} ($status-csh) or $?. The return code is often used in shell scripts to check whether a program terminated correctly or incorrectly.

There are several ways to test return codes. The following code checks for the existence of positional parameter 1 in the passwd file as a login name.

     cj> cat valid_user
     if grep "^$1" /etc/passwd > /dev/null
     then echo "$1 is a valid user." exit 0
     else echo "$1 is NOT a valid user." exit 1

The return code of the grep command is tested by the if command. If the return code is 0 the command is true, else it is false. The grep command searched the /etc/passwd file for a string passed to the shell script as positional parameter 1. If the string matches a string at the beginning of a line (^) in the passwd file, the line is displayed. The indirection of all standard output from the grep command is sent to the system’s trash can (/dev/null), so you do not see the output from the command. If you ran this as the command valid_user, the return code would be 0 if the user was found, and 1 if not found.


Understanding commands is an essential requirement before you can use the UNIX system. Commands are the fundamental part of the interface language that allows you to perform tasks on the computer. The UNIX commands are unlike most commands offered by an operating system. They can be used to perform hundreds of programming tasks to solve everyday problems. It is advisable to become familiar with command syntax, rules, and execution. If you do write shell scripts, be sure to follow the rules outlined in this module. It makes life easier for you and the users in the long run.


In this activity you use the shell to execute internal and external commands. Begin at the shell prompt.

1.  Type cat - file1 > file2 and press Return.
2.  Type the following text; end each line with Return. Press Ctrl-D on the last line to send cat an end-of-file signal. The text you typed is redirected to file2. When you press Ctrl-D, cat begins reading file1. Your input and file1 are redirected to file2. The cat command is an external command.
     cj> cat - file1 > file2
     This input is from my keyboard.
     Cat reads it from the keyboard and the shell redirects it.
3.  Type cat file2 to display the contents of file2. Notice the input you typed from the keyboard and the contents of file1 are displayed.
4.  Type set -- one two three and press Return. This sets the positional parameters of your login shell to "one," "two," and "three." The set command is an internal command.

C Shell
Type set argv=(one two three)

5.  Type echo $* " : " $2 and press Return to display the new positional parameters that you have set by using the set command. A colon followed by the second positional parameter is also displayed. The echo is an internal and external command. If the output is redirected, it is executed as an external command; otherwise, it is executed as an internal command.

C Shell
Type echo $*":"$2 or echo $argv":"$argv[2].

6.  Turn to Module 67 to continue the learning sequence.

Previous Table of Contents Next