好的,所以有些奇怪似乎正在发生。当我以交互方式运行history -r
时,它按预期工作:
$ 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.
但是,从我的.bashrc
运行它并不具有相同的预期行为:
$ 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
我可以通过Freenode上的 shbot 确认:
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
那么这里发生了什么?如何从.bashrc
文件中正常使用它?
更新
用我的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.
使用 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.
那么我的Bash和 shbot 之间可能造成什么差异呢?
答案 0 :(得分:1)
如果设置HISTTIMEFORMAT
env,它是否有效。首先变量,然后再尝试运行历史记录?
export HISTTIMEFORMAT="%F %T "
如果未设置HISTTIMEFORMAT
,则时间戳不会写入历史记录文件中。
答案 1 :(得分:0)
唉!!
我终于确定了我的shell和 shbot 之间的区别,而且确实是我第一次怀疑的Bash 版本,但 shbot 通过我向我撒谎!好吧,实际上它是由于 shbot 的实现细节,但我的命令的这种变化给出了哪些版本工作与否的真实故事:
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
所以现在摘要是指版本!
使用Bash 4.2及更早版本:
Summary interactive bashrc
without HISTTIMEFORMAT Succeeds. Fails.
with HISTTIMEFORMAT Succeeds. Fails.*
*测试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。
从Bash 4.3开始:
Summary interactive bashrc
without HISTTIMEFORMAT Succeeds. Fails.
with HISTTIMEFORMAT Succeeds. Succeeds.*
*测试4.3.18(1) - 发布。
仍然不完全确定为什么HISTTIMEFORMAT
不需要交互式,但需要.bashrc
,但这不是问题。
所以问题的直接答案是:如果使用的Bash版本至少为4.3并且.bashrc
变量设置且不为null,则仅从HISTTIMEFORMAT
解释它们。在我的情况下HISTTIMEFORMAT
已经在我的.bashrc
中设置,因此问题就是版本。显然这是一个错误,这个错误已得到修复。
刚才需要去安装Bash 4.3 ......: - )