Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 19 down vote accepted

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:~$
share|improve this answer
    
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
1  
@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 – Yufenyuy Veyeh Dider May 14 at 8:58

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

share|improve this answer

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.

share|improve this answer
    
You can suggest edits. – muru Jan 8 '15 at 20:24
export PROMPT_COMMAND=date

prints the date before issuing each prompt.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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