UC San Diego SearchMenu

Using the history command

Revised: October 2003.

The UNIX shell maintains a record of recently issued commands and provides convenient means of reusing that information in forming new commands. This feature is known as ``history''. It allows you to repeat commands and to make minor corrections to commands.

Getting a list of recent commands

To display the list of recent commands type:

% history

Repeating commands

To repeat the nth command listed in the history:

% !n

For example, to execute the 8th command in this session type:

% !8

To repeat the last command type:

% !!

To repeat the last command that started with "pat" type:

% !pat

For example, if the most recent command that began with "n" was:

% nroff -ms sample

To repeat it just type:

% !n

Using the last argument of the last command

To use the last argument of the previous command type:

!$

For example, if the previous command was:

% more ltr.application

The file ltr.application can be edited with:

% vi !$

Correcting the previous command

To correct the previous command type:

% ^old^new

For example, if the previous command was:

% ni paper

It could be corrected to:

% vi paper

by typing:

% ^ni^vi

For more information about history, see the manual page for the C-shell by typing:

% man csh

Or, if the Bourne-Again shell is your preferred shell, type:

% man bash