ClimateUnboxed
ClimateUnboxed

Reputation: 8067

simpler way to calculate the minute of the day in bash

I want to know the minute of the day in a bash shell script, and at the moment I can only think to do this by piping two date commands with the hour of day and minute of hour to bc in this way:

mod=$(echo 60*$(date +%H)+$(date +%M) | bc)

echo $mod

This works, but is very clunky and not very elegant, is there a nicer way? I didn't see an option of minute of day in the date command.

I'm using bash version

 4.4.20(1)-release 

Upvotes: 2

Views: 741

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295288

As a more efficient approach (with bash 4.4 or later), albeit not necessarily a shorter one:

printf -v dateMath '%( (10#%H*60)+10#%M )T' -1
mod=$(( dateMath ))

...as a less-efficient one-liner (but still much faster than using date and bc), you could also write this as:

mod=$(( $(printf '%( (10#%H*60)+10#%M )T' -1) ))

Upvotes: 5

Related Questions