Display a running clock in terminal using bash without using loop

Viewed 5363

How to display a running clock in a linux terminal without using any for or while loop. Using these loops in scripts with 1 sec duration causes a significant load in the systems.

4 Answers

If you want to add the date to the terminal window title and your terminal supports it, you can do

#! /bin/bash

while :
do
    printf '\033]0;%s\007' "$(date)"
    sleep 1
done &

and it won't affect any other terminal output.

This post is ancient, but for anyone still looking for this kind of thing:

#!/bin/bash

printf '\033[;H'
while true; do date; sleep 1; done

This version avoids recursion (and stack faults!) while accomplishing the goal.

(For the record - I do prefer the watch version, but since the OP did not like that solution, I offered an improvement on the other solution.)

Related