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.

You might be able to set the $PS1 (prompt) variable to have the time, but I'm not sure if changes.
– kiriOct 17 '13 at 1:32

You can specify \@, but the time will only be recalculated each time the prompt displays, not a current time.
– kiriOct 17 '13 at 1:40

2

@muru, As I specified in my question, that answer only work for zsh not for bash.
– Rosh_ThSep 20 '14 at 7:31

2

But the question itself is not about zsh (it is general), so I advised G_P to post the answer there. With G_P's answer now added there, it qualifies as a dupe.
– muruSep 20 '14 at 7:32

1

@muru that other question specifically wants the time in the prompt. The OP here just wants it printed in the terminal and does not specify that it has to be in the prompt.
– terdon♦Sep 20 '14 at 11:46

8 Answers
8

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.

This question has been bothering me for a few days, and here's my edited post with all the material I've learned. Specifically , my goal was to achieve showing clock next to prompt in BASH. Just like Radu Radeanu mentioned in his answer, the key is to find a function or variable what is able to constantly refresh the command prompt or redraw the screen, without interfering with your screen or with whatever you type. While, this is possible to achieve with while loop, as shown by G_P, the constant redrawing of the screen with tput function messes up with whatever text is on my screen at the instant it refreshes. I've come pretty close to achieving what the OP asked by modifying somewhat the code G_P posted , but still I believe Radu's answer is the closest to what OP wants. Without further ado, here's some of the things I've learned, presented for your judgement.

#1, My preferred solution: Terminal Multiplexer

Terminal multiplexer is a software that allows splitting the terminal screen into two or more screens. In particular, I want to draw attention to Byobu terminal. This ncurses based terminal emulator, allows you to show time, CPU percentage, memory, and whole bunch of other indicators. In the screenshot from my byobu terminal you can see cpu temperature, battery percentage, internet connection quality, CPU frequency, RAM, date, and of course time. If you're mostly concerned with constantly monitoring these things while in terminal, Byobu is the way to go. In fact , I've come to like it so much, I've set it to automatically load while login into tty. The only quirk is that in tty you can't actually merge the separate windows - only switch between tabs with F3 and F4 keys.

An alternate solution involving multiplexing windows is by using splitvt, which splits the screen into two parts. You can run a terminal clock such as tty-clock , use watch command with date, or you can code one yourself with a script. More on that later.
In the screenshot you can see one part of the split with normal bash prompt on top, and in the lower one you can see a script running, that just constantly refreshes output of date command with clear command.

#2 , While Do Loop and tput
This is probably the closent to what the person who asked the question wanted implemented with bash.

G_P has posted a great solution involving the while do and tput commands. However in G_P's code there were two things I didn't like. One, the clock was to the right of the prompt, and two - because sleep cycle is 1 second (refer to the original code where it says sleep), the screen is redrawn every second, and that messes up my output. What I did, is to edit my bash prompt to be one line lower with \n operator and change G_P's code so that tput operator would position the clock right above the prompt, and refresh every 60 seconds; that allows me to see hours and minutes (and I don't exactly care about minutes), while the clock's loop doesn't mess up my screen. First, here's my prompt:

Small edit: I've noticed that this code above doesn't bring up the clock right away sometimes. Here's a better version: while true; do tput sc;tput cup 0 0;date +%R;tput rc; sleep 60; done & This one places clock on screen first, then lets bash know to go through sleep cycle of 60 second

Note, the tput cup 0 0 places clock into upper right corner of the screen, right above my prompt and sleep 60 changes sleep cycle to 60 seconds, because that way I can monitor only hours and minutes instead of every second. This way, whatever text i have on the screen isn't affected, for the most part (only when i happen to run a command the same time clock refreshes).

PS1 line is already in your .bashrc profile, so you just need to tweak it a little. The while do loop should be added somewhere in the same file, preferably in the end with # comment that this is stuff you added initially. And don't forget to always back up any file like that just in case you make bubu and need to go back to the way it was.

#3, Scripting and other

This part is merely for those who want to see time. For those who want to constantly monitor it, #1 and #2 are preferred options.

So as we already have seen the while do loop is pretty useful. Here's how you can create a clock with while do loop, date, and sleep commands.

Hit return/enter, and you basically will see output of date command shown on cleared screen every second. If you don't want to type all that every time, turn all this stuff into script:

#!/bin/bash
while true
do
clear;date
sleep 1
done

Now you can run that script with splitvt in one instance of bash, while working in the other.

Another solution is watch -n1 date command, which will continuously execute date command with period of 1 second.

In Conclusion: I've learned some things while trying to find the answer to this question, and I hope my findings helped you in some way, and I hope I was able to contribute on this question. Byobu terminal IMHO is the best solution, but decide for yourself, make some research, and choose whichever way works best for you.

After else statement the first line is commented out with # (this is my old prompt), now the second line is the one you want, and specifically \t operator. That shows running time in 24 hour format. For more info check out this . Also a small note, I'm using the stuff after else, because i'm not using colored prompt.

PS: Please let me know if I should add anything to my answer or edit it in any way, but for the most part i believe this is most conclusive answer i could come up with, and at least I personally won't be adding anything new to it.

since i prefer displaying as little background info as possible on the terminal i generally customize every session as a #, $, etc. (if you want to change it permanently, minerz029's solution is perfect)

To modify it to show current, I could go for

export PS1='\T$'

for time in 12 hour format. You can view the bash documentation for other options.

I appreciate for your time and effort, but this is not what I want..
– Saurav KumarOct 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.
– kiriOct 17 '13 at 2:01

Well I think you can do this.. :) It seems that you've reached near to the solution..
– Saurav KumarOct 17 '13 at 2:07

why it includes \u two times ? @SauravKumar
– DavidJan 11 '14 at 13:23