y=$(date '+%Y') # Get current year
m=$(date '+%m') # Get current month
((m--)) # Decrement month
[[ ${m} == 0 ]] && ((y--)) # If month zero, decrement year
[[ ${m} == 0 ]] && m=12 # If month zero, reset to 12
cal ${m} ${y} | awk 'NF{A=$NF}END{print A}'
# Run cal, and print the last field of the last line with fields.
this should work if you have gnu date:
<month>
here is current month, for exampleanother example, find the last day of the month before July:
note that, the timestamp was set to
00:00:00
.My comment
date -d 'last month'
was wrong.This works:
Test
Just changed my date to test this way:
Reference: http://databobjr.blogspot.nl/2011/06/get-first-and-last-day-of-month-in-bash.html
It means "Show me the date that is the 1st of the current month minus 1 day"
EDIT:
For MacOS, the args are a bit different
For KSH/BASH