Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I prefer to use terminal and spend most of the time using it. I am searching a way to see time in terminal while I use it parallel. The time would change automatically when it passes. It would be awesome if it would display left to my command prompt.

If this is my command prompt in terminal:

saurav@saurav-P4I45Gx-PE:~$

Then I would like to see clock(time) as:

saurav@saurav-P4I45Gx-PE[06:27:01]:~$

or

saurav@(06:27:01):~$

or any other format. where 06:27:01 is the time. What I want is just to show the time which changes when seconds pass.

So is there any way to achieve this? I will be very happy to get the solution :)

Thanks in advance!!

share|improve this question
    
You might be able to set the $PS1 (prompt) variable to have the time, but I'm not sure if changes. –  minerz029 Oct 17 '13 at 1:32
    
@minerz029: Done!! –  Saurav Kumar Oct 17 '13 at 1:39
    
You can specify \@, but the time will only be recalculated each time the prompt displays, not a current time. –  minerz029 Oct 17 '13 at 1:40
add comment

2 Answers 2

up vote 16 down vote accepted

I'm not sure that is so simple to achieve this using the default bash shell (but I'm not saying that it's impossible). You will probably need a command/function which is able to refresh the command prompt every second without interfering with anything you type on the prompt.

The Z shell (zsh) has a built-in command, called zle, which, when used with the reset-prompt argument, forces the prompt to be re-expanded, then redisplays the edit buffer.

If you want to try it, use the following steps:

  1. Install Z shell with this command:

    sudo apt-get install zsh
    
  2. When you run zsh for the first time, choose 0 when you are asked.

  3. Edit the ~/.zshrc file (by running gedit ~/.zshrc) and add the following lines:

    setopt PROMPT_SUBST
    PROMPT='%B%F{red}%n@%m%f%F{yellow}[%D{%L:%M:%S}]%f:%F{blue}${${(%):-%~}}%f$ %b'
    TMOUT=1
    
    TRAPALRM() {
        zle reset-prompt
    }
    

    Save the file and close it.

  4. In your terminal, when you are still using zsh, run source .zshrc to reset your prompt. Now your prompt should look like:

    saurav@saurav-P4I45Gx-PE[1:25:21]:~$
    

    with some colors.

  5. If you like it, run chsh -s /bin/zsh to change your current shell to /bin/zsh (a re-login is required for this change to take effect).

  6. Run exit if you want to exit from zsh shell.

Here is a 15 second screencast from my terminal:

running clock in terminal before the command prompt

share|improve this answer
    
Bonus points for screencast. Cool stuff indeed. –  MadMike Oct 17 '13 at 11:28
    
Wow!! Working great!! That is why I call you Awesome :) One more thing, how to make it permanent. I,e make it to start when I start the terminal. –  Saurav Kumar Oct 17 '13 at 11:30
    
@SauravKumar Run chsh to change your default shell from /bin/bash to /bin/zsh. You have to log out and log in again. –  Radu Rădeanu Oct 17 '13 at 11:34
    
Actually I've made it.. But every-time terminal opens I've to execute zsh command to enter into this clock mode.. –  Saurav Kumar Oct 17 '13 at 11:36
    
@RaduRădeanu: Yes worked after login as well as restart.. You're great.. Jai Ho!! :P –  Saurav Kumar Oct 17 '13 at 11:44
add comment

This time will only change when a new prompt is executed

You can add the following to your .bashrc:

export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:[\t]:\w\$ "

Which will produce:

USER@HOST:[12:59:59]:~$ _

More information here: http://www.thegeekstuff.com/2008/09/bash-shell-ps1-10-examples-to-make-your-linux-prompt-like-angelina-jolie/#8

Search for "8. Create your own prompt using the available codes for PS1 variable" in that above page.

share|improve this answer
1  
I appreciate for your time and effort, but this is not what I want.. –  Saurav Kumar Oct 17 '13 at 1:51
    
@SauravKumar Yes, I understand you want a running clock. I'm not sure if that's possible with the 'PS1' variable, maybe $PROMPT_COMMAND would work if you can find a command which displays ticking time. –  minerz029 Oct 17 '13 at 2:01
    
Well I think you can do this.. :) It seems that you've reached near to the solution.. –  Saurav Kumar Oct 17 '13 at 2:07
    
why it includes \u two times ? @SauravKumar –  David Jan 11 at 13:23
    
@David: I'm sorry, I don't know. But @minerz029 probably would answer your question. :) –  Saurav Kumar Jan 11 at 13:45
show 3 more comments

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.