Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why do I get different results for the "history" command for the same user after I reconnect a disconnected SSH session?

  • I connect to a server using putty (SSH), say as root
  • My network gets disconnected
  • I reconnect my putty session
  • When I hit my up arrow key to re-run the last used command, it shows a different command

I am guessing this is due to getting reconnected to a different pts. Am I right?

share|improve this question
Possible duplicate: Preserve bash history in multiple terminal windows – slm May 10 '13 at 8:34
You didn't ask here, but if you do want to keep a session running after Putty disconnects, use screen. You can then resume a screen session the next time you log in via ssh. You should have no trouble finding tutorials about using screen. – iyrin Oct 15 '14 at 22:43
up vote 2 down vote accepted

This is because history of your commands from current session are flushed on disk during logging off. And if you has been disconnected, when you connect again you will get last flushed history.

You can also manually flush history to disk by running:

history -a

Refer to man history:

   history [n]
   history -c
   history -d offset
   history -anrw [filename]
   history -p arg [arg ...]
   history -s arg [arg ...]
          With no options, display the command history list with line numbers.  Lines listed with a * have been  modified.
          An  argument  of n lists only the last n lines.  If the shell variable HISTTIMEFORMAT is set and not null, it is
          used as a format string for strftime(3) to display the time stamp associated with each displayed history  entry.
          No intervening blank is printed between the formatted time stamp and the history line.  If filename is supplied,
          it is used as the name of the history file; if not, the value of HISTFILE is used.  Options, if  supplied,  have
          the following meanings:
          -c     Clear the history list by deleting all the entries.
          -d offset
                 Delete the history entry at position offset.
          -a     Append  the ‘‘new’’ history lines (history lines entered since the beginning of the current bash session)
                 to the history file.
          -n     Read the history lines not already read from the history file into the current history list.   These  are
                 lines appended to the history file since the beginning of the current bash session.
          -r     Read the contents of the history file and use them as the current history.
          -w     Write the current history to the history file, overwriting the history file’s contents.
          -p     Perform  history  substitution on the following args and display the result on the standard output.  Does
                 not store the results in the history list.  Each arg must be quoted to disable normal history  expansion.
          -s     Store  the  args  in the history list as a single entry.  The last command in the history list is removed
                 before the args are added.

          If the HISTTIMEFORMAT variable is set, the time stamp information associated with each history entry is  written
          to  the history file, marked with the history comment character.  When the history file is read, lines beginning
          with the history comment character followed immediately by a digit are interpreted as timestamps for the  previ-
          ous  history line.  The return value is 0 unless an invalid option is encountered, an error occurs while reading
          or writing the history file, an invalid offset is supplied as an argument to -d, or the history  expansion  sup-
          plied as an argument to -p fails.
share|improve this answer

There are several environment variables that control history's behavior. Some of them include:


Typically what happens is that you have more than one shell open and which ever shell get's closed last clobbers some of the entries that were written there previously by the shell that you closed first.

Take a look at the Bash Reference Manual's section on the history command for more info.

share|improve this answer

Basically @frzie answers this question but, to possibly clarify, you have a temporary history file that is created when you log on to a shell session. When you log off that history file is appended to the permanent .history file that is stored in $HOME.

Because you haven't logged off, this append didn't happen. To test this, you can issue the command history and compare the output (which includes your temporary history) with the output of cat $HOME/.history (which is the contents of the .history file in your $HOME). These outputs should differ (even if you only see the history command you just issued in one but not the other). Now if you issue the history -a command the files should be more or less the same.

share|improve this answer
Is there an actual file, or is it just in memory? – mattdm Nov 16 '14 at 5:25
@mattdm I believe the 'temporary history' sits in a buffer (in the memory allocated to the shell) until the shell is closed properly. – I-Ii Nov 17 '14 at 8:59
So.... Not a file. :) – mattdm Nov 17 '14 at 11:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.