Bash: value too great for base when using a date as array key
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]="" )
I can't reproduce the problem with associative arrays:
#! /bin/bash
declare -A dailyData
today=2021-02-08
todayData=whatever
dailyData["$today"]="$todayData"
But, if I use normal arrays, i.e. declare -a
(mind the case!) or no declaration at all, then I'm getting the error you mention. That's because the array index is interpreted as an arithmetic expression, so for 2021-02-07, it was just calculated as 2021 - 2 - 7 = 2012, but for 2021-02-08, the last number in the subtraction is invalid in octal.