Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2038521
  • 博文数量: 220
  • 博客积分: 8531
  • 博客等级: 中将
  • 技术积分: 4976
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-18 13:33
文章分类

全部博文(220)

文章存档

2017年(1)

2015年(1)

2014年(5)

2013年(6)

2012年(6)

2011年(30)

2010年(37)

2009年(53)

2008年(41)

2007年(40)

分类: LINUX

2009-10-14 14:33:00

    在bash中,所执行的命令可以用history显示出来,但是bash的机制是:先将命令写入baffer,退出shell时才会写入.bash_history文件中,所以最后.bash_history文件中的命令无法分辨出来时间,解决这个问题可以采用时间戳的方法。
    bash中提供了时间戳的参数HISTTIMEFORMAT

HISTTIMEFORMAT
              If this variable is set and not null, its value is used as a format string for strftime(3) to print the time stamp associated with each history entry displayed by the history builtin. If this variable is set, time stamps are written to the history file so they may be pre-served across shell sessions.


    有了这个参数,可以让执行的命令用history显示时,前面加上时间戳。举例如下:

 1008 2009-10-14 14:31:00 history
 1009 2009-10-14 14:32:23 cat .bash_profile
 1010 2009-10-14 14:36:01 man bash
 1011 2009-10-14 14:37:53 history


    另外就是需要让bash实时写入历史命令。这也有相关参数:

export PROMPT_COMMAND="history -a"         #用以实时写入history
export HISTTIMEFORMAT="%F %T "             #history带时间戳


阅读(2155) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~