2

I would like that each time I run a command in terminal I have the time displayed in my term then the command executed.

  • Do you want the time to be displayed when you run the command (the moment of pressing Enter) or is it enough if the time is displayed in the prompt line, every time the previous command exits or you press Enter on an empty line? – Byte Commander Aug 1 '17 at 10:18
  • 1
8

You can just run

 PROMPT_COMMAND="date"

on your terminal then every time a command is ran on your terminal the current date and time will be printed out on your terminal. As suggested by@pa4080, to make it permanent, add PROMPT_COMMAND="date" to your ~/.bashrc file.

From man bash:

PROMPT_COMMAND

If set, the value is executed as a command prior to issuing each primary prompt.

I think you can use the same command PROMPT_COMMAND= to run almost "anything" you want to run when a command is ran on the terminal.

If you use tmux, it also display time on the corner:

tmux

| improve this answer | |
  • To make it permanent, include PROMPT_COMMAND="date" in ~/.bashrc. – pa4080 Aug 1 '17 at 13:04
  • 3
    But wouldn't that show the date/time when the program terminated and returned to the CLI prompt? – RonJohn Aug 1 '17 at 13:53
  • As stated before, this will display the time when the prompt is displayed, not the time when the command is done. If you really want to see when the time is started, you could do "date;yourcommand" (Albiet, not very convenient). – Brad Aug 1 '17 at 17:08
3

Assuming you're running Bash, you can set PROMPT_COMMAND to a command that is executed when the prompt is printed out. Similarly, you could run an arbitrary command by including a command substitution in the prompt:

$ PS1='[$(date +"%T")] \u@\h \w\$ '
[22:17:54] user@somehost /tmp$ 

But this is common enough that Bash supports it off the shelf

\D{format}
The format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required.

So, similarly to the previous

$ PS1='[\D{%T}] \u@\h \w\$ '

But those print the time the prompt was printed. This may be quite a while before the next command is executed. To get the time the command is started, we can use the DEBUG trap, which runs once for each command line executed.

$ starttime() { date +"Command started at %T"; }
$ trap starttime DEBUG
$ ls -l /dev/null
Command started at 22:25:07
crw-rw-rw- 1 root root 1, 3 Jul 18 21:46 /dev/null

The DEBUG trap could also use the $BASH_COMMAND variable which contains the command being executed, but that's probably not very useful since the command line is visible at the prompt anyway.

| improve this answer | |
1

use zsh terminal, this terminal can show date time run commandenter image description here

https://github.com/robbyrussell/oh-my-zsh/wiki/themes#rkj-repos https://github.com/robbyrussell/oh-my-zsh/wiki/themes#xiong-chiamiov

| improve this answer | |
1
  1. Edit .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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.