I'd like to create a Bash dictionary in a loop, then add it to an array:
#!/usr/bin/env bash
periods=()
for arg
do
echo "arg: $arg"
day= month= year=
case $arg in
*/*/* | *-*-*)
read -r year month day < <(date '+%Y %m %d' -d "$arg")
;;
????[-/]?? | ????[-/]?)
IFS='-/' read -r year month <<< "$arg"
;;
??[-/]???? | ?[-/]????)
IFS='-/' read -r month year <<< "$arg"
;;
esac
declare -A period
period[year]=$year
period[month]=$month
period[day]=$day
periods+=(period)
done
# display
for period in "${!periods[@]}"
do
echo "year: ${period[year]} / month: ${period[month]} / day: ${period[day]}"
done
Unfortunately, it appears each period instance in periods is a reference to the last-added value:
$ ./parse.sh 3/14/19 3/14/2020
arg: 3/14/19
arg: 3/14/2020
year: 2020 / month: 03 / day: 14
year: 2020 / month: 03 / day: 14
Is there a way to define a Bash variable such that each instance is unique?