39

Pressing the up button is tedious and I might miss the command I was looking for. So when typing history at the terminal gives me all the commands I have entered before, indexed to a number. Is there any way I could just call the number associated with the command?

1
  • any more interactive option by now? these are so error prone
    – matanox
    Feb 23, 2018 at 20:50

5 Answers 5

54

The bash history can do many helpful things, and the search with Strg-r that Terry Wang mentioned is an important one among them. But it is also possible to do exactly what you asked for.

You can re-do the previous command with !!. With this, you can also edit the previous command. If for example you forgot to get root privileges for a command

apt-get install a-long-list-of-packages

you don't have to retype all of that again. Instead just call

sudo !!

If you want to re-execute the command at a specific position from your history, you can also use !, for example

!3

to re-execute the command at position 3. Be aware that this counts from the top. So if you're storing 500 commands in your history, !1 would be "500 commands ago". You can also use negative numbers. For example

!-2

would re-execute the second last command.

You can also re-execute the last command that started with a string like

!apt-

which would re-do the last line that started with "apt-". If you want the last command where the string appeared anywhere in the line, you can use something like

!?pt-ge

There are more interesting things the bash history can do. Just to give an impression of the wide range of possibilities, you can specifically access a parameter of a command from history. So

!-5:3:p

would print the third parameter to the fifth from last command.

EDIT: Regarding Rudie's comment below, with the standard settings this bash history expansions are indeed executed directly. It's probably best described like this: A call like !-3 is replaced by the shell with the third last command from your history and then your input (with the replacement) executed. So if you type !-3 and press ENTER and your third last command was ls ~, it's in effect the same as if you typed ls ~ again and pressed ENTER "on your own".

If you don't want that, you can set the shell option histverify. For setting and unsetting shell options, you might want to read up on the shopt command. With histverify set, a call like !-3 only writes the replacement from your history to your command line, but doesn't execute it directly. You have, so to speek, press the crucial ENTER yourself - or refrain from it, if you choose to.

4
  • 1
    So ! is pretty damn dangerous... I'd want to know what !-4 did exactly before running it. Or does it show you first? Cool summary!
    – Rudie
    Feb 19, 2014 at 20:55
  • 1
    @Rudie That's a good point, I updated my answer. The history features are mostly meant to speed up your work and save you keystrokes. So a standard "Do you really, really mean that?" would at least partially defeat that purpose. And even though you could, normally you won't call a "far back" command like !-238. It shouldn't be too hard to remember your second last, third last, maybe fith last command ;) As often with Linux (and, actually, with computing in general), it's expected that you know what you're doing. And if you really want the additional safety net, it's one shopt away ;) Feb 21, 2014 at 11:05
  • I am getting bash: !20471: command not found when I do !20471 when 20471 was the index of my last command... Oct 12, 2022 at 10:20
  • 1
    @Sabito錆兎standswithUkraine That's probably not a problem with the history. My guess would be that the command you've entered previously and now try to repeat isn't available anymore for some reason. Also, a history of more than 20.000 commands is very large. The default is, if I'm not mistaken, 1.000 commands. Maybe you're running into some kind of limit there. Oct 12, 2022 at 10:29
34

Yes, there is. If, for example, you want to execute the command that has number 1234 next to it, do:

!1234

Other useful stuff:

  • If you want to run the last command you ran, do:

    !!
    
  • If you want to run the last ls command you ran, do:

    !ls
    
  • If you just ran ls /some/long/path, and you want to cd to it, do:

    cd !$
    

For more information:

1
8

Using the key combination Ctrl+R, you will be able to use keywords to search your Bash history.

A quick bash key binding reference can be found here: http://ss64.com/bash/syntax-keyboard.html

7

If you've found the history number of a command and you want to execute it again, type ! followed by the number, e.g. !1234 and press Enter. This is part of history expansion; you can tack modifiers onto it.

If you want to edit the command before running it, fc 1234 invokes an external editor.

But as Terry Wang already mentioned there are usually faster ways of locating a command in the history than determining its history number, including Ctrl+R to invoke incremental search (press Enter when you've found the command you want to execute), or fc PREFIX to execute the previous command beginning with PREFIX.

3

The history command will show all commands and their associated numbers. If you want to just do a quick check before executing command line # 135 you can type:

echo !135

This will show you what is going to be executed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .