How can I get last day in the month Shell Scripting [duplicate]
Getting the last day for a month is sometimes tricky, because not all months have the same days number of course.
One way to do it is to get this month's first day, add a month to that (to get next month's first day) and then subtract a day.
date --date="$(date +%Y-%m-01) +1 month -1 day"thu jan 31 00:00:00 -07 2019
And remember that date
can output whatever format you want (argument starting with +
).
date --date="$(date +%Y-%m-01) +1 month -1 day" '+%F'2019-01-31
See man date
for more info or info '(coreutils) date invocation'
for even more.
This gives you last day of previous month
date -d "-$(date +%d) days -0 month"
and this gives you last day of current month
date -d "-$(date +%d) days +1 month"
if [[ $(date +%d%m%Y -d "-$(date +%d) days +1 month") != $(date +%d%m%Y) || $(date +%d%m%Y -d "-$(date +%d) days -0 month") != $(date +%d%m%Y) ]] echo "Today is NOT the last day of the month"else echo "Today is the last day of the month"fi