4.2. The command-line history

Using the command history

Use the up and down key's to scroll through previously typed commands. Press [Enter] to execute them or use the left and right arrow keys to edit the command first. Also see history (below).

The history command

The history command can be used to list Bash's log of the commands you have typed:

This log is called the “history”. To access it type:

history n

This will only list the last n commands. Type “history” (without options) to see the the entire history list.

You can also type !n to execute command number n. Use !! to execute the last command you typed.

!-n will execute the command n times before (in other words !-1 is equivalent to !!).

!string will execute the last command starting with that “string” and !?string? will execute the last command containing the word “string”. For example:

!cd

Will re-run the command that you last typed starting with “cd”.

“ commandName !*” will execute the “commandName” with any arguments you used on your last command. This maybe useful if you make a spelling mistake, for example. If you typed:

emasc /home/fred/mywork.java /tmp/testme.java

In an attempt to execute emacs on the above two files this will obviously fail. So what you can do is type:

emacs !*

This will execute emacs with the arguments that you last typed on the command-line. In other words this is equivalent to typing:

emacs /home/fred/mywork.java /tmp/testme.java
Searching through the Command History ( CTRL-R )

Use the CTRL-R key to perform a “reverse-i-search”. For example, if you wanted to use the command you used the last time you used snort, you would type:

CTRL-R then type “snort”.

What you will see in the console window is:

(reverse-i-search)`':

After you have typed what you are looking for, use the CTRL-R key combination to scroll backward through the history.

Use CTRL-R repeatedly to find every reference to the string you've entered. Once you've found the command you're looking for, use [Enter] to execute it.

Alternatively, using the right or left arrow keys will place the command on an actual command-line so you can edit it.