Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash -value too great for base (error token is "09") for date string yyyymmdd [duplicate]

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!

like image 268
activelearner Avatar asked Dec 14 '22 13:12

activelearner


1 Answers

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")
like image 93
Etan Reisner Avatar answered Jan 22 '23 06:01

Etan Reisner