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?
The bash history can do many helpful things, and the search with
You can re-do the previous command with
you don't have to retype all of that again. Instead just call
If you want to re-execute the command at a specific position from your history, you can also use
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,
would re-execute the second last command.
You can also re-execute the last command that started with a string like
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
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
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
If you don't want that, you can set the shell option
Yes, there is. If, for example, you want to execute the command that has number 1234 next to it, do:
Other useful stuff:
For more information:
Use Key combination
A quick bash key binding reference: http://ss64.com/bash/syntax-keyboard.html
If you've found the history number of a command and you want to execute it again, type
If you want to edit the command before running it,
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