5

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.

3 Answers 3

7

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.

1
  • 1
    OOps!! My answer is almost same as what you put before me. I must say yours is little more informative. No meaning of two equivalent answer. I am deleting mine. +1 from me:)
    – sourav c.
    Commented Dec 13, 2013 at 20:51
1

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.

0

The main differences between the contents of the history file (which is usually ~/.bash_history) and the current history (which is accessible with the up and down arrows and the history command) are:

  • As others have mentioned, the current shell's history is not usually written to the history file until the shell exits.
  • Of equal importance, the current shell's history is not usually read from the history file except when the shell is launched.

To put it another way:

  • Commands in current shell aren't written to the history file until the shell has quit (i.e., it writes them on exit).
  • Commands added to the history file (for example, by another concurrently running shell instance) after the current shell has started running are not accessed by the up and down arrows, nor by typing history and pressing Enter.

However, the history command, which is a shell builtin, has options that change this behavior. You can explicitly tell the shell to read from or write to the history file. Specifically, you can:

  • write this shell's entries to the history file after the existing ones (-a) or instead of them (-w)
  • read the history file and add unread entries (-n) or all entries (-r) to this shell's history
  • clear this shell's history entirely (-c) or just remove a single entry (-d offset)

For more information:

You must log in to answer this question.

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