I’ve acquired a bizarre one right here. I am writing a program that wants entry to bash historical past. I had a have a look at parsing the
.bash_eternal_history in my config) file, however realized the heavy lifting had already been accomplished by the
historical past builtin.
Some contents of my historical past file appear like this. Whereas I may parse this myself, I would like to depend on
historical past‘s standardized parser because it already offers with edge-case multiline instructions, timestamps, histfile codecs, and works cross-platform.
#1587920724 echo check #1587920725 echo test2 #1587920729 contact file #1587920731 rm file #1587920732 historical past
When typing the
historical past command, I get a pleasant listing of timestamped historical past, as follows:
5083 [2020-04-26 18:05:24] echo check 5084 [2020-04-26 18:05:25] echo test2 5085 [2020-04-26 18:05:29] contact file 5086 [2020-04-26 18:05:31] rm file 5087 [2020-04-26 18:05:32] historical past
historical past is a shell builtin, I must run it by way of a bash command string in my program. Nonetheless, when working the command
bash -ic 'historical past -r; historical past' , I’m greeted with this:
10159 [2020-04-26 18:08:35] #1587920724 10160 [2020-04-26 18:08:35] echo check 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] contact 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] historical past
The commented timestamps are printed out as in the event that they have been instructions, and the whole lot has the identical timestamp (the time I ran the command).
The unusual half is, on Linux,
bash -ic 'historical past -r; historical past' produces precisely the identical output as
historical past, within the common format with appropriate timestamps, which is what I anticipated to occur.
I suppose I want to know:
- Why is the output of the 2 instructions totally different in MacOS however the identical in Linux?
- How can I obtain the proper output from a command string? (I’m working the command from a Python script)
- Am I going about this within the fallacious means? My authentic goal was to get the complete timestamped bash historical past from inside a Python program (with out doing one thing exterior like
historical past > historical past.txt && ./script.pyor
historical past | ./script.py).
- MacOS model:
- Bash model:
- Historical past config in
.Profile: Everlasting bash historical past