Under Construction

Selecting an Entry from the History with "!"

Here is the current history that we are assuming:

[user01@aixe01 ~]$ history
    1  ls -l /usr
    2  vi sample.txt
    3  ls -l sample.txt
    4  cat sample.txt
    5  cp sample.txt sample.txt.bak
    6  history
[user01@aixe01 ~]$

First of all, a command must be selected from the history. There are several possibilities for this. The easiest way is to simply specify the number of the command:

[user01@aixe01 ~]$ !4
cat sample.txt
This is a test file!
[user01@aixe01 ~]$

(Note: The result of the expansion is displayed first, here “cat sample.txt“, then the output of the executed command is printed.)

The input line is read (“!4“), due to the occurrence of the history character “!” in the input it is recognized that a history expansion is necessary. The string “!4” is a so-called identifier for a command in the history (event designator). It is replaced by the command with the number 4 during the expansion (in our case “cat sample.txt“). The resulting input line is then further evaluated by the shell and then finally executed.

The string “!4” means much less typing than the command “cat sample.txt“. Of course you could also press the cursor-up key twice in this case, but if the desired command was already some time ago, you would have to press the cursor-up key quite often. “!4” is much faster!

Instead of specifying the absolute number of the command, you can also specify how many entries you have to go back in the history to access the desired command. To do this, enter a minus sign “” after the history expansion character “!” followed by the number of entries that should be gone back in the history. Our history currently looks like this:

[user01@aixe01 ~]$ history
    1  ls -l /usr
    2  vi sample.txt
    3  ls -l sample.txt
    4  cat sample.txt
    5  cp sample.txt sample.txt.bak
    6  history
    7  cat sample.txt
    8  history
[user01@aixe01 ~]$

The last call of “cat sample.txt” is 2 commands ago. So the command can be accessed using “!-2“:

[user01@aixe01 ~]$ !-2
cat sample.txt
This is a test file!
[user01@aixe01 ~]$

It is relatively common for the last command to be started again. According to what has been said so far, this can be done by specifying “!-1“. But since this occurs more frequently, there is a special abbreviation for it, namely “!!“:

[user01@aixe01 ~]$ !!
cat sample.txt
This is a test file!
[user01@aixe01 ~]$

For the last commands, you may remember the order in which they were executed and can refer to them with the relative specification “!-n“. However, this should not usually be the case for commands that date back a long time. Unless someone has an extremely good memory. Therefore there is the possibility to search for a command. To do this, enter the command in question (or a part of it) after the “!“. The history mechanism searches for the last command that begins with the specified character string. E.g. the last command that started with “ca”:

[user01@aixe01 ~]$ !ca
cat sample.txt
This is a test file!
[user01@aixe01 ~]$

If further cat commands with different arguments were called after the desired command, here “cat sample.txt”, the situation becomes a bit more difficult. We assume the following history:

[user01@aixe01 ~]$ history
    1  ls -l /usr
    2  vi sample.txt
    3  ls -l sample.txt
    4  cat sample.txt
    5  cp sample.txt sample.txt.bak
    6  cat sample.txt
    7  cat start.ksh
    8  cat stop.ksh
    9  ls -l
   10  history
[user01@aixe01 ~]$

The command with the number 6 (“cat sample.txt”) should be executed again. Specifying “!ca” from above, would now execute command 8 (“cat stop.ksh“), since this is the last command that starts with the character string “ca“. Also, using “!cat” will execute “cat stop.ksh” and not “cat sample.txt“:

[user01@aixe01 ~]$ !cat
cat stop.ksh sa
this is stop.ksh
[user01@aixe01 ~]$

With the history expansion there is the possibility to search for a character string at any position of a command. To do this, the search string is specified between two question marks “?“. A search for the character string “sam” or “sample” anywhere in the command then leads to the desired command with the number 6 (“cat sample.txt“):

[user01@aixe01 ~]$ !?sam?
cat sample.txt
This is a test file!
[user01@aixe01 ~]$

Note: If the second question mark is immediately followed by a newline, then the second question mark can also be omitted. You can then type “!?sam” as a short cut.

These history event specifiers provide a convenient and easy way to access any past command.

All options are summarized here:

    • !n  The command with the number n.
    • !-n The command n positions before the current command
    • !! The last command, short cut for !-1
    • !string  The last command that begins with the specified string
    • !?string[?] The last command that contains the specified string