Command History is generally a feature provided by bash, and is stored per-user in the .bash_history
file in your home directory.
While the history
command may show you different histories in different terminals while those terminals are open, they will all be merged and written to the common ~/.bash_history
file once you exit the terminals.
I'd like to add another method of modifying (or deleting) history entries, which I found rather accidentally when I was working with the bash:
To demonstrate this, start by executing the following three commands in bash:
$ echo 1
1
$ echo 2
2
$ echo 3
3
You can now select these commands again using the arrow keys or Ctrl+p and Ctrl+n.
Say you want to modify the first two commands. Move through history until echo 1
appears and change it to echo 1 - changed
, but DO NOT PRESS ENTER. If you now move further through your history, this line stays in its modified state and you can move away from and back to it. Now move to the line echo 2
and change it to echo 2 - changed
, again don't press enter. In order to save the changes to these lines, select any command in history except for these two, and hit Ctrl+c.
Of course, instead of modifying the history entry, you may also remove it which will result in an empty line at that entry. To delete the line currently displayed at the prompt, hit Ctrl+e (which jumps to the end of the line) followed by Ctrl+u (which deletes the text from the start of the line to the cursor).
See also https://unix.stackexchange.com/questions/195668/what-can-cause-an-item-to-be-deleted-from-my-bash-history/195726#195726 for a more detailed explanation of the technical background.
Simplest Way:
- ctrl+r to search the command you want to delete/modify.
- end to select the searched command to delete/modify
- Delete or modify the selected command (don't press enter or ctrl + c)
- up arrow(or ctrl+p) or down arrow(or ctrl+n) to select any other command.
- ctrl+c That's it!
Note:
This changes only the current session commands. If we want to change older commands and save the changes we need to run following command before closing the terminal:
history -w
Best Answer
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 commandyou 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
!
, for exampleto 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 examplewould 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
!-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 pressENTER
and your third last command wasls ~
, it's in effect the same as if you typedls ~
again and pressedENTER
"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 theshopt
command. Withhistverify
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 crucialENTER
yourself - or refrain from it, if you choose to.