I set the HISTTIMEFORMAT variable to store the timestamp info .bash_history file. I am able to display the history properly if I use interactive shell.

But, when I run history command to display history, the time stamp info of .bash_history file is is displayed as command and time stamp info of all the commands are set to current time.

Example file contents: bash_history:



$ ssh Host 'HISTFILE=~/bash_history;HISTTIMEFORMAT=" %F %T ";set -o history; history'


235 2015-08-24 13:12 #45556656
236 2015-08-24 13:12 pwd
237 2015-08-24 13:12 #45677756
238 2015-08-24 13:12 ls

Issues: 1. Time stamp is same for all commands. It sets to the current timestamp.

Time info from history file is read as command. So lost the actual time stamp info of commands. How to get the correct info when I try to get the info through ssh or cronjob.

Note: I have seen similar questions but none has given solution to my problem and none, who asked the question, elaborated the scenario.


You also need to enable histexpand, which allows the timestamps to be interpreted properly as they are prefixed with history comment character:

$ ssh HOST 'HISTFILE=/tmp/bash_history;HISTTIMEFORMAT=" %F %T ";set -o histexpand -o history; history'
1   1971-06-12 01:37:36 pwd
2   1971-06-13 11:15:56 ls
3   1971-06-13 07:56:05 who

See the description of HISTTIMEFORMAT and set -H in the bash(1) manpage for more details.

Note, the order appears to be important. Here's what happens with history before histexpand:

$ ssh pocampo 'HISTFILE=/tmp/bash_history; HISTTIMEFORMAT=" %F %T ";set -o history -o histexpand; history'
1  ??#45556656
2  ??pwd
3  ??#45677756
4  ??ls
5  ??#45665765
6  ??who
  • Wow !! it worked like charm. I am googling for 5 days for this solution and thought of doing in more complex way by converting timestamps manually. Thanks for your solution. – Rajasekhar Aug 26 '15 at 6:37
  • @ganducoder, Check this solution for your question. – Rajasekhar Aug 26 '15 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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