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
4.4.20(1)-release
CodePudding user response:
As a more efficient approach (with bash 4.4 or later), albeit not necessarily a shorter one:
printf -v dateMath '%( (%H*60) %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 '%( (%H*60) %M )T' -1) ))