Up arrow and .bash_history have different commands, so where does the up arrow get its data?
I seem to have discovered that the ~/.bash_history
file doesn't contain what is displayed when you use the up arrow. I found this by doing a command like so
smbclient -L 10.2.10.240 -umyuser -p"mypassword on the command line is bad"
This command is actually wrong, it has a mistake, the -u
is actually supposed to be a -U
and so this command failed.
However I also realized that that was bad to do, because it puts your password into the history, I went into .bash_history
and what was interesting was that it doesn't have the mistake. It only has
smbclient -L 10.2.10.240
and that's it. But this is were it gets weird, if you use the up arrow the whole command including mistakes is there for you to edit.
So the question is, why is there a difference between what is in .bash_history
and what the up arrow displays? Where does the up arrow data live?
Before you point it out; it's quite convenient in this case because it prevents my password from being saved, but, that's not why it's doing this, I also tested it with correct options and then it does save the whole command including passwords and all.
Solution 1:
The ~/.bash_history
file is only updated when you close/exit from the terminal. While you are in the terminal you can browse backwards in commands which was entered in that session. So if you exit from that terminal session your history will be updated with the missing command.
Also if you put a space at the beginning of a line, before the command, that command won't be saved in the history. This is a good practice if you issue a command which you don't want to be included in the history.
Solution 2:
Your question is answered but in addition, if you want to enter the realtime data in history you enter: history
entering !directly followed by the history number re-executed the command, in the man pages you will find some nice ways to alter your command before executing.