How can I get the 1st and last date of the previous month in a Bash script?

后端 未结 7 1637
小蘑菇
小蘑菇 2020-12-31 03:52

I have scheduled a Bash script to run on the 1st of the month but I need to create 2 variables in it with the 1st and last date of the previous month, whatever those may be.

7条回答
  •  囚心锁ツ
    2020-12-31 04:19

    If you're doing this on the 1st day of the month then you can use something like

    first=$(date --date='-1 month')
    last=$(date --date='-1 day')
    

    But if you're running on another date then I guess you'll need to start from a known reference date.

提交回复
热议问题