如果我从我的 .bashrc 运行"history -r",为什么不会解释时间戳?



好吧,似乎有些奇怪的事情发生了。当我以交互方式运行history -r时,它按预期工作:

$ printf %b '#1401928364nechon' > /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.

然而,从我的.bashrc运行它没有相同的预期行为:

$ printf %b 'history -r /tmp/histnhistory 3n' > /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

我可以在Freenode上用shbot来确认:

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

这是怎么回事?;我怎么能让它从我的.bashrc文件正常工作?

更新:

With my shell:

$ echo "$BASH_VERSION"; printf %b '#1401928364nechon' > /tmp/hist; printf %b 'export HISTTIMEFORMAT=x27%F%a%T  x27nhistory -r /tmp/histnhistory 3n' > /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.
与<<p> em> shbot :
19:45:29 <JamesJRH> # echo "$BASH_VERSION"; printf %b '#1401928364nechon' > /tmp/hist; printf %b 'export HISTTIMEFORMAT=x27%F%a%T  x27nhistory -r /tmp/histnhistory 3n' > /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 '#1401928364nechon' > /tmp/hist; printf %b 'export HISTTIMEFORMAT=x27%F%a%T  x27nhistory -r /tmp/histnhistory 3n' > /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 '#1401928364nechon' > /tmp/hist; printf %b 'export HISTTIMEFORMAT=x27%F%a%T  x27nhistory -r /tmp/histnhistory 3n' > /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.
那么是什么导致了Bash和shbot之间的差异呢?

设置HISTTIMEFORMAT环境是否有效?变量,然后再运行历史记录?

export HISTTIMEFORMAT="%F %T "

如果未设置HISTTIMEFORMAT,则不将时间戳写入历史文件。

唉!!

我终于确定了我的shell和shbot的区别,它确实是Bash 版本,正如我最初怀疑的那样,但是shbot欺骗了我!实际上,这是由于shbot的实现细节,但是我的命令的这个变体给出了哪些版本工作或不工作的真实故事:

03:31:11 <JamesJRH> # printf %b '#1401928364nechon' > /tmp/hist; printf %b 'echo "$BASH_VERSION"nexport HISTTIMEFORMAT=x27%F%a%T  x27nhistory -r /tmp/histnhistory 3n' > /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 '#1401928364nechon' > /tmp/hist; printf %b 'echo "$BASH_VERSION"nexport HISTTIMEFORMAT=x27%F%a%T  x27nhistory -r /tmp/histnhistory 3n' > /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

所以现在摘要指向版本!

使用Bash 4.2及更早版本:

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

* 4.2.47测试(2)释放,4.2.45(1)释放,4.2.25(1)释放,4.2.8(1)释放,4.1.11(1)释放,4.0.38(1)释放,3.2.51(2)释放,3.2.39(1)释放。

从Bash 4.3开始:

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

*测试4.3.18(1)-释放。

仍然不完全确定为什么HISTTIMEFORMAT不需要交互,但在.bashrc中需要,但这不是问题。

因此,对这个问题的直接回答是:只有当使用的Bash版本至少为4.3并且HISTTIMEFORMAT变量被设置且不为空时,才会从.bashrc解释它们。在我的情况下,HISTTIMEFORMAT已经在我的.bashrc中设置了,所以这是版本的问题。很明显这是一个bug,这个bug已经被修复了。

现在需要去安装Bash 4.3…:-)

最新更新