date command displays current date and time to the Terminal and we can use this command to recursively display time every second on Terminal which will look like a Terminal Clock. In this tutorial, I will show you how to create a Shell Script which will make the terminal to show system time every second like a clock.
Default output of date command prints result in the format shown in following image.
But if we use +%r option with date command, the output format changes to format of the system locale as shown in following image.
Instructions:
- Tell the Terminal to use
bashto execute the commands of the script file.
#!/bin/bash
- Now clear the Terminal using
clearcommand so that output of thedatecommand will shown at the top of the Terminal window.
clear
- Now using a
whileloop which will run infinitely print the time using the following command in whichechocommand is used with-eoption which enable interpretation of backslash escapes (\b) and\coption prevent theechocommand to enter a newline character.
echo -e "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b`date +%r` \c"
- We have to sleep the Shell for a second before next iteration of
whileloop takes place. So, the full while loop looks like
while true do echo -e "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b`date +%r` \c" sleep 1 done
Following image is a screenshot of the working sample script.
You can download the sample script file here.


