A clock is nothing special, we have tons of it but when it comes to linux the joy of accomplishing anything on command line is heavenly. In this document we are going to see how to display a digital clock in terminal (which updates). You can see the current data/time with Linux’s default command date. We will be playing with this very command to do the intended. To retrieve only the time from date command, we can add “+%r” or “+%T” to date command.
With this “data” command you can print the system time. The logic behing building a digital clock is to get continuos output. One way to do this is to use “watch” command along with “date” command. Thus to get a continuous digital clock you can execute following code:
watch -n 1 date +%r
You will find following result:
Now, if you want a clear and formatted output of current time then you can go for this second method. This time we are going to use “echo” command to display the date and in addition to that, we will put it in infinite loop which will update every 1 second- giving a feel of continuous digital clock.
The code goes like this:
clear; while true; do echo -e \\b\\b\\b\\b\\b\\b\\b\\b`date +%T`\\c ; sleep 1; done
You will see something similar to this:
Let’s break our code down:
is used for displaying the output of “date” command.
is to delete the previous output. We have used “%T” with date which gives 8 characters long output. That is why we have used
8 times. In case you wish to use “%r”, you will have to use
for 11 times as it produces detailed output.
is used to check “echo” from creating new line.