I am trying to generate a startDate in the format yyyymmdd such that this date is the 1st day of the third last month. For example, the current month is July (07), so I need to get my start date as the 1st day of April (04) - 20150401.
In order to achieve this in bash, I used the following command:
StartDate=$(date -d "-3 month -$(($(date +%d)-1)) days" +"%Y%m%d")
However, this is giving me the following error:
line 93: 09: value too great for base (error token is "09")
I do not want to change the format of the StartDate variable. For single digit months (1-9), the format should have a leading zero infront of the month number in the yyyymmdd format.
Is there something that I am missing?
Thanks in advance!
bash is seeing 09
and trying to treat it as an octal number which fails.
You can prefix the date value with 10#
to force a base. See the second-to-last paragraph of Shell Arithmetic.
StartDate=$(date -d "-3 month -$((10#$(date +%d)-1)) days" +"%Y%m%d")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With