简体   繁体   English

如何在终端/控制台的命令行旁边显示时间

[英]How to show time next to the command line in terminal/console

When I execute command line (linux) I want to know what time there were executed when I scroll up my terminal window.当我执行命令行 (linux) 时,我想知道向上滚动终端窗口时执行的时间。 I saw this once setup in linux environment but how to do that?我曾经在 linux 环境中看到过这个设置,但是如何做到这一点?

命令行前的时间

You need to set your prompt variable ( PS1 ).您需要设置提示变量 ( PS1 )。 Something like the following should get you going:像下面这样的东西应该让你去:

<~/temp>$ export PS1="[\$(date +%k:%M)]> "
[12:16]> 

You can setup PS1 to always show current time in BASH;您可以将PS1设置为始终在 BASH 中显示当前时间;

export PS1='\A-\w>'
  • \\A - current time stamp without seconds part \\A - 没有秒部分的当前时间戳
  • \\t - current time stamp with seconds \\t - 带秒的当前时间戳
  • \\w - current directory \\w - 当前目录

如果您想以以下格式显示它:

YYYY-MM-DD HH:MM:SS {domain}@{userId}:~/{folder1}/{folder2}$

export PS1='\D{%F %T} \$\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$'

1) Open bashrc file 1) 打开 .bashrc 文件

gedit ~\.bashrc

2) Find the following text: 2) 找出以下文字:

if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] :\[\033[01;34m\]\w\[\033[00m\]\$ ' 

3) And replace with: 3)并替换为:

if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\] [\d|\t]:\[\033[01;34m\]\w\[\033[00m\]\$ ' 

4) Restart terminal to check. 4) 重启终端进行检查。

Sample output in terminal终端中的示例输出

Right now I have my terminal with this configuration:现在我的终端具有以下配置:

[hh:mm:ss] PC@User:~$ [hh:mm:ss] PC@User:~$

This can be achieved putting the following instruction on file ~/.bashrc这可以通过将以下指令放在文件 ~/.bashrc 上来实现

export PS1="[\\$(date +%k:%M:%S)] [\\e]0;\\u@\\h: \\w\\a]${debian_chroot:+($debian_chroot)}\\u@\\h:\\w\\$ " export PS1="[\\$(date +%k:%M:%S)] [\\e]0;\\u@\\h: \\w\\a]${debian_chroot:+($debian_chroot)}\\u@ \\h:\\w\\$ "

I hope it works for you.我希望这个对你有用。

更简单的解决方案

export PS1='\D{%F %T} \u@\h:\W\$'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM