Sleeping until top of the minute

By David
/
First published 23rd June 2017 (Last Modified 9th January 2021)

I recently wanted my bash script (running on Ubuntu) to sleep until the top of the minute. The suggested code snippet from goggling is:

sleep $((60 - $(date +%S) ))

This works well, unless the current seconds count is 08 or 09. Bash interprets numbers starting with 0 as octal and 08 (and 09) are not valid octal numbers. I fixed this in my script as follows:

sleep $((160 - 1$(date +%S) ))