Unix Notes

Date entertainment

get last month using bash and the date command

MON=`date +”%m”`
-bash: ((: MON=09: value too great for base (error token is “09”)

So, do this

MON=`date +”10#%m”`
echo $MON

How entertaining – bash treats the leading zero as an indicator of octal.

Same would need to be done for any of the `date` formats that return leading zero as part of the result.

to get the seconds since EPOCH for other reasons –

date -r $((`date ‘+%s’` – 86400))

This version has no problem with the boundary condition of month number 1

MYDATE=`date +”%Y%m”`
MONTH=$((10#${MYDATE:4:2} – 1))
[[ $MONTH == 0 ]] && MONTH=12 && ((YEAR–))