I want the previous month & previous year in the format: "December2020".
But while executing the below Linux command I’m getting July2020.
echo `date -d "2021-01-08" '+%B' -d 'last month'``date -d '1 year ago' +%Y
The date hardcoded parameter is in format: YYYY-MM-DD (2021-01-08)
date accepts only one
-d option. In your command
date -d "2021-01-08" '+%B' -d 'last month' the first
-d is ignored. Only the
-d "last month" applies. Because of that, and since we have August right now, the output is
You probably wanted to use
date -d '2021-01-08 - 1 month' +%B%Y
If you really wanted to concat the previous month and previous year together, you could use
echo "$(date -d'2021-01-08 - 1 month' +%B)$(date -d'2021-01-08 - 1 year' +%Y)"
but that would give rather strange results:
2021-01-08 → December2020 # 1 month before input date 2021-04-30 → March2020 # 13 months before input date
Answered By – Socowi