1

I've got a weird one here. I'm writing a program that needs access to bash history. I had a look at parsing the .bash_history (or .bash_eternal_history in my config) file, but realized the heavy lifting had already been done by the history builtin.

Some contents of my history file look like this. While I could parse this myself, I would prefer to rely on history's standardized parser as it already deals with edge-case multiline commands, timestamps, histfile formats, and works cross-platform.

#1587920724
echo test
#1587920725
echo test2
#1587920729
touch file
#1587920731
rm file
#1587920732
history

When typing the history command, I get a nice list of timestamped history, as follows:

 5083  [2020-04-26 18:05:24] echo test
 5084  [2020-04-26 18:05:25] echo test2
 5085  [2020-04-26 18:05:29] touch file
 5086  [2020-04-26 18:05:31] rm file
 5087  [2020-04-26 18:05:32] history

As history is a shell builtin, I need to run it through a bash command string in my program. However, when running the command bash -ic 'history -r; history' , I am greeted with this:

10159  [2020-04-26 18:08:35] #1587920724
10160  [2020-04-26 18:08:35] echo test
10161  [2020-04-26 18:08:35] #1587920725
10162  [2020-04-26 18:08:35] echo test2
10163  [2020-04-26 18:08:35] #1587920729
10164  [2020-04-26 18:08:35] touch file
10165  [2020-04-26 18:08:35] #1587920731
10166  [2020-04-26 18:08:35] rm file
10167  [2020-04-26 18:08:35] #1587920732
10168  [2020-04-26 18:08:35] history

The commented timestamps are printed out as if they were commands, and everything has the same timestamp (the time I ran the command).

The strange part is, on Linux, bash -ic 'history -r; history' produces exactly the same output as history, in the regular format with correct timestamps, which is what I expected to happen.

I guess I would like to know:

  1. Why is the output of the two commands different in MacOS but the same in Linux?
  2. How can I achieve the correct output from a command string? (I am running the command from a Python script)
  3. Am I going about this in the wrong way? My original aim was to get the full timestamped bash history from within a Python program (without doing something external like history > history.txt && ./script.py or history | ./script.py).

Thanks :)

Additional information

  • MacOS version: 10.14.6 Mojave
  • MacOS bash version: 3.2.57
  • History config: Eternal bash history
  • Linux version: Linux Mint 19.2
  • Linux bash version: 4.4.20
1

I can confirm the issue.

Yes, there seems to be a problem with bash version 3.2.57 and before in that they don't recognize the timestamps in the history file. I was unable to find the specific change in the bash changes file. You probably need to upgrade bash to a newer version (4+).

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.