4

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:

#45556656
pwd
#45677756
ls
#45665765
who

Command:

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

Output:

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.

0

1 Answer 1

2

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
2
  • Correct me if I'm wrong, but I believe this to be a very serious security issue - if your bash history file is located in /tmp/bash_history, any program with basic privileges would be able to read it. May 25, 2020 at 23:20
  • Correct, it would be a security concern if that location was used regularly/permanently. The commands above were generated and stored in a temporary location for use in this example. Apologies if the override of HISTFILE caused confusion.
    – zackse
    May 27, 2020 at 1:44

You must log in to answer this question.

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