简体   繁体   中英

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. I saw this once setup in linux environment but how to do that?

命令行前的时间

You need to set your prompt variable ( 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;

export PS1='\A-\w>'
  • \\A - current time stamp without seconds part
  • \\t - current time stamp with seconds
  • \\w - current directory

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

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

gedit ~\.bashrc

2) Find the following text:

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:

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.

Sample output in terminal

Right now I have my terminal with this configuration:

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

This can be achieved putting the following instruction on file ~/.bashrc

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\$'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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