Bash – Handling ‘Value Too Great for Base’ Error When Using Date as Array Key

associative arraybashstring

I have read about specifying "10#", but I don't think it's my case as I am not doing number comparison. I am trying to create an associative array in Bash, and the code worked fine until today (2021-02-08):

dailyData["$today"]="$todayData"

$today is a day in ISO format, $todayData is not relevant.
I am getting the error 2021-02-08: value too great for base (error token is "08").

Why does Bash interpret this date format as a number, where an arbitrary string does the job (associative array key)?
What if I wanted to use just "08" as dictionary key?

Best Answer

It's because dailyData is being automatically created as indexed array rather than an associative array. From man bash:

An indexed array is created automatically if any variable is assigned to using the syntax name[subscript]=value. The subscript is treated as an arithmetic expression that must evaluate to a number.

The issue goes away if you explicitly declare dailyData as an associative array:

$ declare -A dailyData[2021-02-08]="$todayData"

$ declare -p dailyData
declare -A dailyData=([2021-02-08]="" )
Related Question