

The former is inadequate because due to the small time spent doing the printing, the output will not actually happen once per second but a bit less than that which is suboptimal. Instead, the proposed solutions either use a sleep 1 between subsequent timer outputs or a busy loop that outputs as fast as possible. I'm surprised that nobody used the sleepenh tool in their scripts. In bash, add these lines to your ~/.bashrc (the sleep 0.1 will make the system wait for 1/10th of a second between each run so you don't spam your CPU): countdown() ))Ĭombine this with some way of playing sound in linux terminal ( Play MP3 or WAV file via the Linux command line) or Cygwin ( cat /path/foo.wav > /dev/dsp works for me in Babun/Windows 7) and you have a simple flexible timer with alarm! You can combine these into simple commands by using bash (or whichever shell you prefer) functions. If you need greater precision, you can use this to give you nanoseconds: while true do printf '%s\r' "$(date +%H:%M:%S:%N)" doneįinally, if you really, really want "stopwatch format", where everything starts at 0 and starts growing, you could do something like this: start=$(date +%s)įor a countdown timer (which is not what your original question asked for) you could do this (change seconds accordingly): seconds=20 That will show you the seconds passing in realtime and you can stop it with Ctrl+ C. If all you want is a stopwatch, you can do this: while true do printf '%s\r' "$(date)" done
