自动计时每个执行的命令并在 Bash 提示符中显示?
我经常忘记在执行时显式地添加“time”命令前缀,理想情况下我会在下一个 shell 提示中看到最后一个命令花费了多少实时时间(在每个命令上)。
我已经浏览了 bash 文档,但找不到任何相关内容。
I often happen to forget to explicitly prefix executions with the "time" command, ideally I would see in the next shell prompt how much real time the last command took (on every command).
I already scoured through the bash documentation, but couldn't find anything related.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
或者将其放入您的
~/.inputrc
中:然后当您想要执行
time sleep 1
时,您可以输入sleep 1
> 并按 Ctrl+J 而不是 Enter。我不建议在绑定命令(或 .inputrc 文件)中交换
j
和m
。 每次按 Enter 时,您都会添加时间
,这可能非常烦人,并且在键入多行命令时会导致错误。您可以将其添加到您的
~/.bashrc
中,以使time
的输出更加紧凑:(类似于我的答案 此处。)
You could do this:
Or put this in your
~/.inputrc
:Then when you want to do
time sleep 1
you'd typesleep 1
and press Ctrl+J instead of Enter.I would not recommend swapping the
j
andm
in the bind command (or in the .inputrc file). Every time you'd press Enter you'd gettime
added which could be pretty annoying and would cause errors when typing a multi-line command.You could add this to your
~/.bashrc
to make the output oftime
more compact:(similar to my answer here.)
另一个 stackoverflow 线程 涵盖了本质上相同的问题。 我在该线程中的回答可以概括为:
...将秒数显示为整数,或者:
..对于“Ddays,HH:MM:SS”,删除前导空值。
Another stackoverflow thread covers the essentially the same question. My answer in that thread can be summarized as:
...to display the number of seconds as an integer, or:
..for "Ddays,HH:MM:SS" with leading empty values removed.