History
The history command lists what commands have been run before, either in the current shell session or in others (by the same user). It generally stores this information in the .<shellname>_history file in the user’s home directory. This can be useful for auditing and for checking on what commands have been run, but it is not a security feature; the file is plain text and can easily be edited by anybody with permission to access the file.
Recalling Commands
You can recall commands in a number of ways; the standard, most basic way is to run the history command. That displays the commands stored in the current history buffer, with a number to the left of each one:
$ history | tail -7
557 pwd
558 cd
559 cd bin
560 ls
561 cat dos2unix
562 id
563 history
$
These can be recalled with the bang, or pling, or exclamation mark (!) symbol, so typing !562 executes the id program again. Alternatively, !-n recalls the last-but-n command, so from this starting point, !-1 (or its shortcut, !!) would call history again, !-2 would call id, and !-3 would call cat dos2unix.
Searching History
There are three main ways to interactively search through the history buffer. The first is with the arrow keys; press the up arrow to scroll backwards and the down arrow to scroll forwards. When you get to a command you want to execute again, either press Enter to execute it exactly as before, or press the left or right arrows to start editing the command before re-executing it. ...
Get Shell Scripting: Expert Recipes for Linux, Bash, and More now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.