106

I was wondering if it was possible to add timestamps to terminal prompts, and if so, how could I achieve this?

muru
  • 197,895
  • 55
  • 485
  • 740
Jan Gorzny
  • 1,163

12 Answers12

109

Add this line to the ~/.bashrc file:

export PROMPT_COMMAND="echo -n \[\$(date +%H:%M:%S)\]\ "

So the output will be something like:

[07:00:31] user@name:~$
heemayl
  • 91,753
devav2
  • 36,312
  • Sometimes I experince that the time is not correct and is older, like there is some kind of delay. I though I could use it to see how long certain commands take (grunt..). How can that be? – Kevin Simper Mar 19 '14 at 15:12
  • 4
    @devav2: Is there a way to specify the timezone? – Hippo Jul 25 '15 at 23:05
  • Is there a way you could also add time stamp to the terminal when you are using root? I only have the time stamp when i am a normal user. Thanks – ThunderBird May 14 '16 at 08:58
  • 6
    For the date command, you can replace +%H:%M:%S with +%T. – snapfractalpop Jun 20 '16 at 17:44
  • 13
    This screwed with my terminal wrapping, so I used the approach detailed here instead. It is just add '\D{%T}' to your PS1 – rbennett485 Jul 11 '16 at 10:40
  • 1
    Also for timezones you need export TZ='Europe/London' (or equivalent) in your ~/.bashrc. You can find the timezone string you need with tzselect – rbennett485 Jul 11 '16 at 10:53
  • @KevinSimper it's not wrong, but it's very easy to forget that the time in the prompt is the time the last command finished, it's not the time that line was started. – Levi H Aug 28 '19 at 10:13
  • @rbennett485 The reason it messed up the wrapping is because the terminal considers them characters. You need to add \[ \] around any non-printed characters to tell the terminal to ignore their length. – Levi H Aug 28 '19 at 10:18
  • With this command, the cursor goes ahead of the text in the terminal (at least in Yakuake in PopOS) very often and it is impossible to write. – KLaz Oct 28 '22 at 10:03
  • How do I add color to this? – b-ak Feb 06 '24 at 02:50
54

My solution was to use http://bashrcgenerator.com/ to generate this PS1 line to put in .bashrc:

export PS1="\t [\u@\h \W]\\$ \[$(tput sgr0)\]"

Which will look like

13:05:54 [chad@work-laptop:~/Downloads]$ ls -al

Using PROMPT_COMMAND messes up the history for me. When a longer line comes up while cycling through the history, it cuts off the end of the prompt by the same number of characters as the timestamp that was added to the front. e.g.

13:14:38 [chad@work-laptop:~/Doexport PROMPT_COMMAND="echo -n \[\$(date +%H:%M:%S)\\] "

And this line can't be edited because it does not display the characters in the right place, as in you aren't typing where it looks like you're typing.

I'm guessing it could be done with PROMPT_COMMAND, maybe by using that [$(tput sgr0)] part, but PS1 works.

dogtato
  • 731
  • 6
  • 5
17

Instead of adding the date to the prompt itself, you could add the date just before your prompt by placing the following line at the top of your .bashrc. For it work you will also need to install cowsay. It's a fun way of displaying the date while keeping a short prompt:

cowsay "Welcome $USER! It's now $(date '+%A %B %d %Y %r')"

In its current form it will work on anyone's system without amendment as it reads the $USER and the date variable from the current environment.

enter image description here

  • 21
    The point of putting it in the prompt is to know when the last time you executed a specific command was. – James Oltmans Apr 03 '17 at 21:15
  • @JamesOltmans It's surprising that you had to point this out. The mind boggles ... – Homunculus Reticulli Mar 18 '21 at 07:22
  • 1
    I love how this answer got this many upvotes. In my headcanon, they're all trolls that want you to see a new cow every time you hit in your prompt or execute a command, even though this answer wouldn't do that. :D :D :D – msb Jul 14 '23 at 17:56
8

The easiest syntax to show a timestamp in a command prompt would probably be:

    PS1='\D{%F} \t $...'

where

\D{format} is date formatted (see man bash, PROMPTING section) as

%F full date; same as %Y-%m-%d (see man date, FORMAT section), and

\t is the current time in 24-hour HH:MM:SS format (see man bash, PROMPTING section)

nano ~/.bashrc

make changes to PS1 variables as shown above

source ~/.bashrc
  • 2
    This is the answer that worked for me. Many others seemed to cause issues where the cursor would occasionally overlap with some of the prompt characters when copying, pasting, or selecting recent commands using the up arrow key. – TheIntern Nov 26 '20 at 10:54
  • Or PS1='\D{%F %T} $ '. (\D{%T} instead of \t) – Weekend Feb 07 '22 at 13:17
8

Since I don't have enough reputation points to add a comment to the answer to improve it. It would seem that the only way I can add information is to create a second answer. So, I will repeat what was said as the answer and build on that.

Edit the .bashrc file for the user that you want to have the date stamp modified for.

If you see "user@host:~$" you're in your current user's home directory. The .bashrc file is a hidden file since it is prefixed with the dot ".". To see it in a list you will need to use a long listing.

You can use "ls -a" and you should then see the ".bashrc" file.

Edit the file with a text editor. For exmaple, "nano .bashrc", "vim .bashrc", "vi .bashrc", "pico .bashrc" or whatever editor you wish to use.

If you want to script this and plan on adding it to many shells for many users it may be beneficial to use the "echo" command in conjunction with the append ">>" operator. For example,

echo "export PROMPT_COMMAND="echo -n \[\$(date +%H:%M:%S)\]\\"" >> .bashrc

If you use that method be sure that you are in the current working directory of the user that you wish to modify it for. To check this you can use the "pwd" or print working directory command.

If you don't like the extra space between the "]" end bracket and the username just use this very slightly modified regex:

export PROMPT_COMMAND="echo -n \[\$(date +%H:%M:%S)\]\\"

Use this if you're directly editing the file.

Avlaxis
  • 81
  • 1
  • 2
4
export PROMPT_COMMAND=date

prints the date before issuing each prompt.

wojox
  • 11,362
3

Open your .bashrc via

vim ~/.bashrc

and then add in the following to .bashrc:

#PS1="[\A \u@\h \W\a]$" 

where \A is for time (\W for only end of path, remove if you want the entire current working directory path). Then type

source ~/.bashrc

You will see something like this:

[16:04 trwood@cobalt04 ~/MCEq_dev]$
jokerdino
  • 41,320
trwood
  • 31
1
export PROMPT_COMMAND="echo -n \[\$(date +%F-%T)\]\ "
export HISTTIMEFORMAT="%F-%T "

at least looks handy for me.

1

I realize I'm a little late to this party, but by setting PS1 in $HOME/.bashrc like so:

PS1="\e[1;38;5;56;48;5;234m\u \e[38;5;240mon \e[1;38;5;28;48;5;234m\h \e[38;5;240mat \e[1;38;5;56;48;5;234m\[\$(date +'%a %-d %b %Y %H:%M:%S %Z')\\] \e[0m\n\e[0;38;5;56;48;5;234m[\w]\e[0m "

I get a full-colour-formatted, time-stampped prompt like so: terminal screenshot

Here's how it breaks down:

  • \e[1;38;5;56;48;5;234m\u is the colour-coded username, where \u would be just the username
  • \e[1;38;5;28;48;5;234m\h is the colour-coded hostname, \h is the hostname
  • \e[1;38;5;56;48;5;234m\[\$(date +'%a %-d %b %Y %H:%M:%S %Z')\\] is the colour-coded full-date-and-time insert in 24-hour time where [\$(date +'%a %-d %b %Y %H:%M:%S %Z')\\] would be the same, but monochrome, and, alternatively, you could just use \d to print the date in your default system format
  • \n moves to the next line
  • [\w] shows the current working directory encased in square brackets

If you want solid tips for using colour codes, I used this guide as a reference for my colour codes.

  • Man that colour scheme is wack, there is so little contrast. You have to have your screen brightness set to ludicrous brightness. I have no clue what folder you are in, cd test dir gives it away but man seriously? – AndrewRMillar Sep 07 '23 at 06:39
  • It's not as bad as it first seems because the terminal is itself semi-transparent and I can read everything just fine against my desktop wallpaper, plus my primary monitor is 32 inches, and at 1920x1080, everything is super big. Also, I know my colour scheme is a little ... obnoxious ... to some, but I included a link to the guide for colour codes to allow the opportunity for full chromatic customization to taste. – EvilSupahFly Dec 27 '23 at 20:59
0

I think the correct way to proceed is to locate in $HOME/.bashrc the codes that sets PS1, comment them and add the personal ones. This is my block for example:

PS1_DATE="\D{%Y-%m-%d %H:%M:%S}"

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

Output:

[2019-06-18 15:54:29 ~/Desktop $] echo "It's late!"
Marco Sulla
  • 662
  • 3
  • 11
  • 30
0

My time is similar to the cowsay answer above. You need to put now your ~/.bashrc but you can also call it from the terminal by typing now.

Here's a screenshot:

now.png

The bash code is posted in this very detailed answer:

0

Just to add to @devav2 's answer:

An easier-to-read echo command for newbies would be:

export PROMPT_COMMAND='echo -n "$(date +%H:%M:%S) "'

Instead of

export PROMPT_COMMAND="echo -n \[\$(date +%H:%M:%S)\]\ "

Becasue the echo command is not meant to be evaluated at the time of the export, it makes more sense to use single quotes for literal quoting rather than double quotes. Using single quotes to quote the entire command then allows you to use double quotes to quote the arguement to the echo command, eliminating the need to escape special characters like , [ and $

qwertyzw
  • 111
  • 2
    The escapes are being used for formatting (the brackets). I don't see what this adds to the accepted answer. – Elder Geek Aug 22 '17 at 16:32
  • You can add them to 'echo -n "$(date +%H:%M:%S) "' too: 'echo -n "[$(date +%H:%M:%S)] "' without needing to escape them. The advantage to using a string as the arguement to the echo command is that now you don't need to escape what is otherwise bash evaluatable, making it easier to read. – qwertyzw Aug 22 '17 at 16:49
  • @ElderGeek I added further clarification. I hope that helps. – qwertyzw Aug 22 '17 at 16:56