I have noticed that when I go up through my commands using the arrow keys, UP and DOWN, that if I then edit a command, it edits it in history, and I can even remove a command like that, however I do not want it to have this affect on the commands in history, I don't want it to actually delete them in the history, only allow me to edit what I am typing, but not actually modify the history through this. So how can I make it so that this does not actually modify the history, but just the command that I execute?
I asked something quite similar in Unix & Linux: How to undo changes in the commands-history?.
The great answer I got from Gilles says:
As long as you've edited a history entry but not pressed Enter yet, to go back to the original entry, repeatedly press Ctrl+_ — the
undocommand — until it doesn't make any further change. You're back to the original entry.
I encountered the same problem and while I was playing around and struggling to understand what was happening, I just figured out the following:
If you call from the
history a command line that you have actually typed earlier in that same current terminal session and then edit it (maybe deleting it) but then interrupt your edit by leaving that (history-)command line using for instance the
Down arrow to come back to the prompt of the actual current command line, then later on when you exit the terminal the edits get recorded in the
This behavior does not happen if you edit and interrupt your edit on a command line that was already recorded in the
.bash_history file before you started the terminal session.
In other words, while a terminal session is open, the newly typed command lines are only kept in memory, not in the
.bash_history. You can recall them using the
up arrow or invoking the
history commands, and you can edit them while the session is open (when invoking the
historycommand, the lines of modified commands will then be indicated by an asterisk
*). It's only when you exit the session that the commands of the session get recorded 'as is' in the
.bash_history file, thereby keeping the last edits you have made on them before exiting the session.
See also the answer from falconer there.