Unix Concepts   «Prev  Next»
Lesson 6Modifying the previous command
ObjectiveUse the history feature to modify the previous command.

Modifying previous unix Command

History substitution lets you modify a command and replay the result. Although you can modify any previous command, the simplest techniques apply to the most recent command.

Quick substitution

For simple fixes to the previous command, use quick substitution, which has the following form:
% ^old^new

You start by typing a caret (^), followed by the old text, which is the pattern you want to change. Type another ^ and then the new text to use.
For example, suppose you mistype a file name while entering a command:
% cp indxe.html index.html.backup
indxe.html: no such file or directory

Instead of re-entering the whole command, use quick substitution to change xe to ex.
% ^xe^ex

Word substitution

Word substitution lets you grab part of a previous command and reuse this piece in a new command. This feature has many forms.
Rather than covering them all, I will cover the !$ combination, which substitutes the last argument of the previous command.
Suppose you want to move three files named red, blue, and green, to another directory. You might issue the following command:
% cp red blue green ~/work/projects/color 

Now suppose you want to cd to this directory. Instead of retyping the long pathname, you can save time by using the !$ word substitution:
% cd !$ 
The !$ will be replaced by the last argument of the previous command. In this case, entering cd !$ is the same as entering:
% cd ~/work/projects/color
In the next lesson, changing your system prompt will be discussed.

Modify Command Exercise

Click the Exercise link below to practice using the history feature.
Modify Command Exercise