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:
PS1='${debian_chroot:+($debian_chroot)}\n[*\u@Ubuntu*]\n $(date) :\w\$ '
And here's G_P's modified code: enter code here
while sleep 60;do tput sc;tput cup 0 0;date +%R;tput rc;done &
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.
Some of the things I've found while researching:
My original post, the minimalistic solution: Edit your ~/.bashrc file. I prefer editing it with nano, so
nano ~/.bashrc
Here's the portion of my own edited file:
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\$'
else
# PS1='${debian_chroot:+($debian_chroot)}[*\u@Ubuntu*]:\w\$ '
PS1='${debian_chroot:+($debian_chroot)}[*\u@Ubuntu*]\t:\w\$ '
fi
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.
$PS1
(prompt) variable to have the time, but I'm not sure if changes.\@
, but the time will only be recalculated each time the prompt displays, not a current time.