The original hackers who invented the tools used under Unix hated waiting around for things to happen. Being a hacker is all about finding the most efficient way of doing any particular task. (For the record, this is why many Unix fans dislike GUIs: messing around with mice and icons seems like a slow way of doing what can be done far more quickly at the command-line shell.)
Because of this, the BASH shell includes many features designed to optimize the user experience. The most important of these is the command history. All this means is that BASH remembers every command you enter (even the ones that don't work!) and stores them as a list on your hard disk.
During any BASH session, you can cycle through this history using the up and down cursor keys. Pressing the up arrow key takes you back into the command history, and pressing the down arrow key takes you forward.
The uses of the command history are enormous. For example, rather than type that long command that runs a program with command options, you can simply use the cursor keys to locate it in the history and press Enter.
On my SUSE Linux test system, BASH remembers 1,000 commands. You can view all of the remembered commands by typing history at the command prompt. This will scroll off the screen because it's so large, but you can use the scroll bars of the Konsole window to read it.
Each command in the history list is assigned a number. You can run any of the history commands by preceding their number with an exclamation mark, referred to as a bang, or sometimes a shriek. For example, you might type !923. On my test system, command number 923 in the BASH history is cd .., so this has the effect of switching me into the parent directory.
Command numbering remains in place until you log out (close the Konsole window or end a virtual console session). After this, the numbering is reordered. There will still be 1,000 commands, but the last command you entered before logging out will be at the end of the list, and the numbering will work back 1,000 places until the first command in the history list.
One neat trick is to type two bangs: !!. This tells BASH to repeat the last command you entered. There are more tricks you can use. For example, typing !-3 will cause BASH to move three paces back in the history file and run that command. In other words, it will run what you entered three commands ago.
Rather than specifying a command number, you can type something like !cd. This will cause BASH to look in the history file, find the last instance of a command line that started with cd, and then run it.
Pressing Ctrl+R lets you search the command history from the command prompt. This particular tool can be tricky to get used to, however. As soon as you start typing, BASH will autocomplete the command based on matches found in the history file, starting with the last command in the history. What you type appears before the colon, while the autocompletion appears afterwards.
Because BASH autocompletes as you type, things can get a little confusing when you're working with the command history, particularly if it initially gets the match wrong. For example, typing cd will show the last instance of the use of cd, as in the example in Figure 18-2. This might not be what you're looking for, so you must keep typing the command you do want until it autocompletes, despite BASH often making several incorrect assumptions.
Figure 18-2. BASH history completion is very useful but can also be confusing.