History Lesson
Command Line – Bash History

© Lead Image © Scott Rothstein, 123RF.com
You can do more with the Bash history command than just using the arrow keys. We show you how to use this command-line tool more efficiently.
If you use the command line regularly, you probably use the Bash history file. Usually located in ~/.bash_history
, the history file stores hundreds of previous shell commands, enabling you to reuse them without typing them. If you are like most users, your use of Bash history is largely confined to using the arrow keys to scroll back one or two commands. Yet the history
command itself offers dozens of ways to search and reuse the history more efficiently: the regular command options, history expansions, environment variables, and a couple of alternative search commands as well.
The limited use of Bash history may now be common partly because many users today have little command-line experience. Even more importantly, the information available in the history
command's man and info pages is verbose and only partial [1]. At best, users pick up isolated tips from how-to articles, which give them little sense of the command as a whole.
Still another reason for not making full use of the history
command is the uncertainty about all it can do. For one thing, the command is controlled by several automatically installed environmental variables that users may never see unless they open ~/.bashrc
. For another thing, by default, the file collects every entry at the command line, whether the entry is properly formed or not. Additionally, entries from every terminal opened after the first terminal are not collected, but not written, which means the history of all except the original terminal are lost when the window is closed. Such quirks are logical, but arcane enough that it quickly makes the history
command a mystery to the average user. What follows is an attempt to systematically explain the different ways you can use history more exactly.
[...]
Buy this article as PDF
(incl. VAT)