Showing posts with label bc. Show all posts
Showing posts with label bc. Show all posts

Get Current Month As Number in BASH

Use bc to convert month from current date to eliminate character zero ('0') when returned month is under 10.
This useful when you need to do some numeric operation using month.

sample :

# retrieve month ( assume current date is September, 17 2012 )
M1=`date -%m` # result: 09
M2=`date -%m|bc` # result: 9

# numeric operation
let "M1=$M1-1" # error: -bash: let: M1 = 09: value too great for base (error token is "09")

let "M2=$M2-1" # result: $M2 is 8