繁体   English   中英

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

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

当我执行命令行 (linux) 时,我想知道向上滚动终端窗口时执行的时间。 我曾经在 linux 环境中看到过这个设置,但是如何做到这一点?

命令行前的时间

您需要设置提示变量 ( PS1 )。 像下面这样的东西应该让你去:

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

您可以将PS1设置为始终在 BASH 中显示当前时间;

export PS1='\A-\w>'
  • \\A - 没有秒部分的当前时间戳
  • \\t - 带秒的当前时间戳
  • \\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) 打开 .bashrc 文件

gedit ~\.bashrc

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)并替换为:

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) 重启终端进行检查。

终端中的示例输出

现在我的终端具有以下配置:

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

这可以通过将以下指令放在文件 ~/.bashrc 上来实现

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

我希望这个对你有用。

更简单的解决方案

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

暂无
暂无

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

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