Using Commands from Your History
Use the ! character to begin a reference to an event in the history list. The shell sees the !, determines from the characters following it which event you want to use, performs a history substitution on the command line, and then executes the command. To keep you informed, the shell echoes the resulting command before executing it. Here's a simple example using !!, the "repeat previous command" operator:
%dateType a command Tue Mar 7 13:01:25 CST 1995 Output from the command %!!Repeat the command date Shell shows the resulting command Tue Mar 7 13:01:32 CST 1995 Output from the command
The full syntax for a history reference looks like this:
!event:words:modifiers
That's a mouthful, but most of it is optional. In practice, history references tend to be simple, such as !! to repeat the previous command or !$ to repeat its last argument. Here's what the parts of a history reference mean:
| The event (command) you want to refer to. |
| Which words to select from the command. |
| How to modify the selected event or words. |
The following sections describe the parts of history references in more detail.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access