Unix Concepts   «Prev  Next»
Lesson 5Repeating previous commands
ObjectiveUse history substitution to repeat previous commands.

Repeating Previous Commands in Unix

Command history lets you recall specific commands and replay them on the command line. This technique is called history substitution and is a great time saver. History substitutions begin with an exclamation point (!).You can use them anywhere in a command line, but it is easiest to use them at the beginning.
The following table summarizes the most basic forms of history substitution:

Command form What it does
% !! Repeats the previous command
% !N Repeats command number N from the history list
% !pattern Repeats the most recent command that starts with pattern

Sample uses are shown in the MouseOver below:
History Substitution s
  1. The history command displays your recent commands. This list is helpful when you want to make history substitutions.
  2. The !3 tells the shell to re-execute your third command, or ls -F in this case. Note that commands are displayed before they rerun.
  3. The !d tells the shell to re-execute the most recent command beginning with ‘d’. In this case, the date command is repeated.
  4. The !! tells the shell to re-execute the previous command, which is the date command in this case

Repeating Previous Commands
In the next lesson, you will learn how to modify the previous command and replay it.