0

Okay, so something weird seems to be going on. When I run history -r interactively, it works as expected:

$ printf %b '#1401928364\necho\n' > /tmp/hist  # Make a test history file with timestamp information.
$ history -r /tmp/hist  # Read it into the current history.
$ history 3  # Bash has interpreted the history comment character such that timestamps don't appear on their own line.
  220  2014-06-05Thu17:59:55  history -r /tmp/hist  # Read it into the current history.
  221  2014-06-05Thu00:32:44  echo
  222  2014-06-05Thu18:00:07  history 3  # Bash has interpreted the history comment character such that timestamps don't appear on their own line.

However, running it from my .bashrc doesn't have the same expected behaviour:

$ printf %b 'history -r /tmp/hist\nhistory 3\n' > /tmp/bashrc  # Make a test bashrc file.
$ bash --rcfile /tmp/bashrc  # Run Bash with the test bashrc file; doesn't interpret the history comment character and prints a line containing ‘#1401928364’.
    1  #1401928364
    2  echo

I can confirm this with shbot on Freenode:

17:29:45 <JamesJRH> # printf %b '#1401928364\necho\n' > /tmp/hist; history -r /tmp/hist; history 3
17:29:47 <shbot>     1  printf %b '#1401928364\necho\n' > /tmp/hist; history -r /tmp/hist; history 3
17:29:47 <shbot>     2  echo
17:29:49 <JamesJRH> # printf %b '#1401928364\necho\n' > /tmp/hist; printf %b 'history -r /tmp/hist\nhistory 3\n' > /tmp/bashrc; bash --rcfile /tmp/bashrc
17:29:55 <shbot>     1  #1401928364
17:29:55 <shbot>     2  echo

So what's going on here?; how can I get it to work properly from my .bashrc file?

Update:

With my shell:

$ echo "$BASH_VERSION"; printf %b '#1401928364\necho\n' > /tmp/hist; printf %b 'export HISTTIMEFORMAT=\x27%F%a%T  \x27\nhistory -r /tmp/hist\nhistory 3\n' > /tmp/bashrc; bash --rcfile /tmp/bashrc
4.2.45(1)-release
    1  2014-06-05Thu19:42:31  #1401928364
    2  2014-06-05Thu19:42:31  echo

Summary                 interactive  bashrc
without HISTTIMEFORMAT  Succeeds.    Fails.
with HISTTIMEFORMAT     Succeeds.    Fails.

With shbot:

19:45:29 <JamesJRH> # echo "$BASH_VERSION"; printf %b '#1401928364\necho\n' > /tmp/hist; printf %b 'export HISTTIMEFORMAT=\x27%F%a%T  \x27\nhistory -r /tmp/hist\nhistory 3\n' > /tmp/bashrc; bash --rcfile /tmp/bashrc
19:45:36 <shbot> 4.3.18(1)-release
19:45:36 <shbot>     1  2014-06-05Thu00:32:44  echo
19:45:49 <JamesJRH> 42# echo "$BASH_VERSION"; printf %b '#1401928364\necho\n' > /tmp/hist; printf %b 'export HISTTIMEFORMAT=\x27%F%a%T  \x27\nhistory -r /tmp/hist\nhistory 3\n' > /tmp/bashrc; bash --rcfile /tmp/bashrc
19:45:56 <shbot> 4.2.47(2)-release
19:45:56 <shbot>     1  2014-06-05Thu00:32:44  echo
19:46:08 <JamesJRH> 41# echo "$BASH_VERSION"; printf %b '#1401928364\necho\n' > /tmp/hist; printf %b 'export HISTTIMEFORMAT=\x27%F%a%T  \x27\nhistory -r /tmp/hist\nhistory 3\n' > /tmp/bashrc; bash --rcfile /tmp/bashrc
19:46:15 <shbot> 4.1.11(1)-release
19:46:15 <shbot>     1  2014-06-05Thu00:32:44  echo

Summary                 interactive  bashrc
without HISTTIMEFORMAT  Succeeds.    Fails.
with HISTTIMEFORMAT     Succeeds.    Succeeds.

So what could be causing this difference between my Bash and shbot's?

James Haigh
  • 1,192
  • 1
  • 12
  • 25

2 Answers2

1

Does it work if you set the HISTTIMEFORMAT env. variable first, and then you try to run history again?

export HISTTIMEFORMAT="%F %T "

If HISTTIMEFORMAT is not set, then the timestamps are not written in the history file.

jimm-cl
  • 5,124
  • 2
  • 25
  • 27
  • No. In my real `.bashrc` I have `export HISTTIMEFORMAT='%F%a%T '` before the reading part. Also, it evidently works without it when reading a history file interactively (see the _shbot_ reply). According to Bash's manpage, if `HISTTIMEFORMAT` isn't null then it will save timestamps to the history file. It shouldn't need it to be set in order to read in the history file, it's just that `history` won't print them. Have you tried it? – James Haigh Jun 05 '14 at 18:43
  • On the other hand, this does work for _shbot_, just not in _my_ `.bashrc`... confusing. I'll try to pin down what's different. – James Haigh Jun 05 '14 at 18:49
  • I couldn't pin it down; I tried on different versions of Bash for _shbot_, but for all versions it works for _shbot_ but not for me. – James Haigh Jun 05 '14 at 20:01
0

Alas!!

I have finally pinned down the difference between my shell and that of shbot, and it was indeed the Bash version as I had first suspected, but shbot through me off the trail by lying to me! Well, actually it's due to an implementation detail of shbot, but this variation on my commands gives the true story of which versions work or not:

03:31:11 <JamesJRH> # printf %b '#1401928364\necho\n' > /tmp/hist; printf %b 'echo "$BASH_VERSION"\nexport HISTTIMEFORMAT=\x27%F%a%T  \x27\nhistory -r /tmp/hist\nhistory 3\n' > /tmp/bashrc; bash --rcfile /tmp/bashrc
03:31:18 <shbot> 4.3.18(1)-release
03:31:18 <shbot>     1  2014-06-05Thu00:32:44  echo
03:31:25 <JamesJRH> # printf %b '#1401928364\necho\n' > /tmp/hist; printf %b 'echo "$BASH_VERSION"\nexport HISTTIMEFORMAT=\x27%F%a%T  \x27\nhistory -r /tmp/hist\nhistory 3\n' > /tmp/bashrc; bash42 --rcfile /tmp/bashrc
03:31:32 <shbot> 4.2.47(2)-release
03:31:32 <shbot>     1  2014-06-06Fri03:31:24  #1401928364
03:31:32 <shbot>     2  2014-06-06Fri03:31:24  echo

So now the summaries refer to versions!

With Bash 4.2 and earlier:

Summary                 interactive  bashrc
without HISTTIMEFORMAT  Succeeds.    Fails.
with HISTTIMEFORMAT     Succeeds.    Fails.*

* Tested on 4.2.47(2)-release, 4.2.45(1)-release, 4.2.25(1)-release, 4.2.8(1)-release, 4.1.11(1)-release, 4.0.38(1)-release, 3.2.51(2)-release, 3.2.39(1)-release.

From Bash 4.3 onwards:

Summary                 interactive  bashrc
without HISTTIMEFORMAT  Succeeds.    Fails.
with HISTTIMEFORMAT     Succeeds.    Succeeds.*

* Tested on 4.3.18(1)-release.

Still not entirely sure why HISTTIMEFORMAT isn't needed interactively but is needed in the .bashrc, but that isn't a problem.

So the direct answer to the question: They are only interpreted from the .bashrc if both the version of Bash used is at least 4.3 and the HISTTIMEFORMAT variable is set and not null. In my case HISTTIMEFORMAT was already set in my .bashrc so it was the version that was the problem. Clearly this is a bug and this bug has been fixed.

Just now need to go and install Bash 4.3… :-)

James Haigh
  • 1,192
  • 1
  • 12
  • 25