如何设置我的 tcsh 提示符以显示上一个命令的执行时间?

How to set my tcsh prompt to show the execution time of the previous command?

我在工作中使用 tcsh,非常希望知道命令的执行时间。

例如:

~
❯ sleep 55

~ took 55s
❯

只需使用time命令即可。

> time sleep 55
0.001u 0.000s 0:55.01 0.0%  0+0k 0+0io 0pf+0w

编辑:

我没注意到标题。要为每个命令执行此操作,您可以使用 precmd, postcmd aliases along with the date 命令

alias postcmd 'set start_time=`date +%s`'
alias precmd 'set end_time=`date +%s`; @ cmd_time= $end_time - $start_time; echo took $cmd_time seconds'

运行 这两个命令,或者将它们添加到 ~/.cshrc 如果您希望这是默认行为。输出将是这样的:

> time sleep 55
took 55 seconds

解释:

postcmd:每个命令执行前 运行 秒。

precmd:运行s 就在打印每个提示之前(在命令运行之后)。

date +%s:以原始秒格式打印时间。

我只是将原始秒值保存在变量 start_timeend_time 中,然后将差异保存在 cmd_time 中并打印出来。