Csh automatically keeps a record of all commands that you have executed recently. To view this record, you need to execute the command history:

% history 
1 echo Genesis 
2 echo Grace 
3 date 
4 pwd 
5 ls 
As you can see, each command has a number assigned to it; these numbers indicate the order in which the commands were executed.

Command Re-execution

During a Unix session, we may need to execute the same few commands many times. Instead of typing these commands at the prompt again and again, we can speed up our work by using the numbers specified by the history command to recall them. A command in the history can be rerun by using the character ! in one of the following forms:

Form Action
!! The last command that was executed is re-executed
!number The numberth command is re-executed
!-number The numberth most recent command is re-executed (!-1 is the same as !!)
!prefix The most recent command that starts with prefix is re-executed

where prefix is a string, and number is always positive. Csh first displays the command called for re-execution, and then it executes it. When using !, you should note the following:

Here are some command re-execution examples:
% history 
1 pwd 
2 echo Hello World! 
3 date 
4 ls 

% !1 
pwd 
/ugrad0/cs219/Labs 

% !da 
Wed Jul 5 15:05:17 PDT 2000 

% !-3 
ls 
dir1/ dir2/ home.gif practice.txt 

Numbering the Prompt

If we do not want to use the history command to know the number assigned to each command we have executed, then we can set the prompt to display the number of each command. This is done by changing the value of the variable $prompt to '\!promptsign', where promptsign is the sign that we want to use to represent the prompt (e.g. % in csh). Here is a sample output with a numbered prompt:

% set prompt = '\!%' 
1% echo Genesis 
Genesis 
2% echo Grace 
Grace 
3% pwd 
/ugrad0/cs219/Labs 
4% !! 
pwd 
/ugrad0/cs219/Labs 
5% !2 
Grace 
You can use ! in all of its forms with the prompt number to recall commands.

Termination and Exit Codes

Every csh command terminates with an exit value. By convention, an exit value of zero means that the command was executed successfully, and a nonzero exit value indicates failure. In csh, the variable $status contains the exit code of the command last executed. In the following example, the pwd command succeeded, whereas the rm command failed:

% pwd 
/ugrad0/cs219/Labs/Cshell 
% echo $status 
0 

% rm someFile.C 
someFile.C: No such file or directory 
% echo $status 
2 

An exit code can be also user-defined. For example, a script can return a designated number to indicate that something went wrong. To do this, the script needs to contain the command exit in the following form:

exit number
where number is the number that should be used as an exit code.

When csh encounters this command in a script, it terminates the script and returns number. Thus, if this statement appears at the end of the script, then one knows whether or not the script has been run and terminated properly. If a script does not explicitly return an exit code, then by default, csh returns the exit code of the last executed command.