Previous Table of Contents Next


Module 64
history (csh, ksh)

DESCRIPTION

The aliased history command displays your command history file. Each command you enter can be saved in a command history file. You can later access this history file to reexecute a previous command. It is common practice to alias h for history. The Korn shell alias and csh alias lines follow:

     alias h=history     # Korn shell
     alias h history     # C shell

You want to place these commands in your .profile or .kshrc if you use the Korn shell (ksh) or in your .login or .cshrc if you use the C shell (csh).

When you log out of the system your history file is removed unless you inform the shell to save it.

COMMAND FORMAT

Following is the general format of the history command.

     history [ -nr ] [ -b [ -e ] ]
     history [ -nr ] [ b [ e ] ]

C Shell
history [ n ]
history -r [ n ]
history -h [ n ]

Options

The following options may be used to control how the history command functions.

-b Beginning point. The number of lines to go back from the last command to start listing out the history file. For example,
                    history -25
lists the last 25 commands saved in the history file.
-e Ending point. The number of lines to go back from the last command to end listing out the history file. For example,
                    history -25 -5
lists out the last commands starting 25 commands ago down to 5 commands ago.
-n Suppress the leading command numbers from the history listing.
-r Reverse the order of history file for displaying. The latest commands issued are displayed first.

C Shell
-h Same as ksh -n option. This may prove useful if you wish to create a file you can source or . (dot) into your local shell.
-r Same as ksh -r option

Arguments

The following list describes the arguments that may be passed to the history command.

b List commands starting at line b in the history file. For instance,
                    history 15
displays the commands beginning at line 15 to the end of the history file.
e List commands up to line e in the history file. For example,
                    history 20 30
displays lines 20 through 30 of the history file.

C Shell
n Displays the n most recent commands (events) stored in the history file.

Variables

There are two variables that control the location and size of the history file. The HISTFILE variable informs the shell where to locate the history file. For example,

     HISTFILE=$HOME/.history

places the history file in your HOME directory in the .history file. If you do not specify path in the HISTFILE variable, the default for the location is $HOME/.sh_history.

The HISTSIZE variable is used to set the number of commands stored in the history file. If you do not set the HISTSIZE variable, it defaults to 128. Therefore, the history file will contain the last 128 commands you have entered.


C Shell
The C shell uses different variables than the Korn shell. To control the size of the file, you use the history variable. For example,
     set history=100

would cause the shell to save the last 100 commands in the history file.

The savehist variable is available in the csh to save n commands in your history file when you log out. The next time you log back into the system those n commands are still available in your history file. If you do not set savehist to a number of history commands to save, your history file is recreated each time you log in.

You can set the savehist variable by placing the set savehist=50 command in your .login file. This will save the last 50 commands in your history file when you log out of the system. The next time you log in, the last 50 commands will be available in your history file.

Another variable may be set to change the history access characters. By default you type ! or ^ to access a previous command residing in the history file. You can change these characters to alternate characters by setting the histchar variable. For example,

     set histchar=.,

changes your history substitute characters to a period (.) and a comma (,). Now you type a .34 to execute the 34th command in your history file. To perform quick substitution on the last command executed, use the , notation instead of the caret (^). For example, type ,-xa,-la, to change xa to la in your last command.

If the verbose variable is set, each command is displayed after history substitution is performed. You set this variable using the -v option of the csh.


RELATED COMMANDS

The read command will place its input in the history file if you specify the -s option (ksh only). Refer to Module 112 for further discussion on the read command.

RELATED FILES

The history file is located based on the HISTFILE variable (ksh only). Usually, you set this variable to your home directory. For example, type

     HISTFILE=~/.history

to place your history file in your home directory in a hidden file. It is common practice to place this variable assignment in your .profile file. The default history file is $HOME/.sh_history.

APPLICATIONS

The history command is used to list previous commands so you can reuse them using the r command (fc -e - in ksh) or the ! notation (in csh). If you forget what number a command is assigned, you can use the history command to list the commands so you can locate the appropriate number.

TYPICAL OPERATION

In this activity you use the history command to list the history file. Begin at the shell prompt.

1.  Type history -23 and press Return. Notice the last 23 commands of your history file are displayed on your screen.

C Shell
If you use the csh, type history 23 and press Return to display the last 23 commands.

2.  Turn to Module 50 to continue the learning sequence.


Previous Table of Contents Next