Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Completely new to bash and having trouble working out how to make the arithmetic work. I think this should be easy, it's just.. argh.

I want to express the time of day in terms of 1/1000ths (ie, midday = 500, etc.)

I figured the script would need to find the number of seconds elapsed so far and then divide that number by 86.4, then return the resulting DDD

Any help? thanks so much

share|improve this question

First, here's how to show the number of hours, minutes and seconds elapsed since the beginning of the day:

date +%H:%M:%S

Since the display format can be customized, output an arithmetic expression that calculates the total number of seconds since midnight, and place that in a shell arithmetic expression construct.

s=$(($(date +"%H*3600+%M*60+%S")))

This returns a number of fractions of 1/86400th of a day. To get 1/1000ths, perform a little more arithmetic:

t=$(($s * 1000 / 86400))

which can be reduced to

t=$(($s * 5 / 432))
share|improve this answer
Thanks, this is exactly what I was trying to do in my head. So, I've gotten as far as: #!/bin/bash clear s=$(($(date +"%H * 3600 + %M * 60 + %S"))) t=$(($s * 5 / 432)) ...but, having just starting trying to learn about bash this morning, I'm not sure where to go from here to output a number. DO I need some kind of echo command? I'm using this script with text expander and so far what I've got isn't producing anything viable... – Jack Anders Sep 28 '11 at 22:36
echo $t after the calulations doesn't give you the correct ouput? – frogstarr78 Sep 29 '11 at 1:58
@Jack Anders: using echo "$varname" works, as does printf "%d" "$varname" (use %d,%s, etc as need be)... 'printf' requires a bit more typing, but is much more flexible, and avoids an occasional quirk of 'echo' in which it cannot differentiate between an option (eg: -n or -e and what it is supposed to 'echo'; when the first word of text to be echoed is the same as the option. (many utilities allow you to use -- to indicate end-of-options. However 'echo' does not have this '--' option. – Peter.O Sep 29 '11 at 3:26
Okay! wow. Thanks everyone for your help, I've put together a script that creates the timestamps I need: #!/bin/bash clear s=$(($(date +"%H*3600+%M*60+%S"))) t=$(($s * 5 / 432)) d=$(date +%j) y=$(date +%Y) printf "$y`$d`$t" "$y" "$d" "$t"' But when the time is rendered, I need it to always display in three digits (ie, oo1, 098...), even if the number is less than 100. Any suggestion about how I could accomplish that? – Jack Anders Sep 29 '11 at 14:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.