Since you want to learn more about associative arrays, have a look at this script, the way you define your associative array is time consuming; that way is more suited to adding new key value pairs to an existing associative array.
I added some formatting to increase the readability of the output.
Wish I got here earlier, the accepted answer built upon things from others... You left them hanging for 20 days, oh well, kudos to them.
Script
Divider="---------------------------------------------" declare -A Locations Locations=( [Etc/UTC]="UTC" [America/Santiago]="Chile" [Pacific/Auckland]="New Zealand" [Africa/Johannesburg]="South Africa" [Atlantic/Stanley]="Falkland Islands" [Asia/Dubai]="Port Alfred, Iles Crozet" [America/Argentina/Ushuaia]="Tierra Del Fuego" [Australia/Perth]="Western Australia, Australia" [Australia/Adelaide]="South Australia, Australia" [Australia/Melbourne]="VIC, NSW, ACT, and QLD, Australia" [Atlantic/South_Georgia]="South Georgia and the South Sandwich Islands" ) getLocationDateTime() { local location label timeDate location="$1" label="$2" timeDate=$(TZ="$location" date "+%H:%M%p, %A, %d %B, %Y") echo -e "$label:\n$timeDate\n$Divider" | sed 's#^# #g' } echo -e "\n Location Date & Times" echo " $Divider" for Location in "${!Locations[@]}"; do getLocationDateTime "$Location" "${Locations[$Location]}" done echo '' Output:
It displays fully and much more nicely on the terminal even when not in full screen, the font here is quite big.
λ bash date.sh Location Date & Times --------------------------------------------- Port Alfred, Iles Crozet: 05:25am, Thursday, 17 October, 2024 --------------------------------------------- New Zealand: 14:25pm, Thursday, 17 October, 2024 --------------------------------------------- Western Australia, Australia: 09:25am, Thursday, 17 October, 2024 --------------------------------------------- UTC: 01:25am, Thursday, 17 October, 2024 --------------------------------------------- South Australia, Australia: 11:55am, Thursday, 17 October, 2024 --------------------------------------------- Chile: 22:25pm, Wednesday, 16 October, 2024 --------------------------------------------- South Georgia and the South Sandwich Islands: 23:25pm, Wednesday, 16 October, 2024 --------------------------------------------- VIC, NSW, ACT, and QLD, Australia: 12:25pm, Thursday, 17 October, 2024 --------------------------------------------- Falkland Islands: 22:25pm, Wednesday, 16 October, 2024 --------------------------------------------- Tierra Del Fuego: 22:25pm, Wednesday, 16 October, 2024 --------------------------------------------- South Africa: 03:25am, Thursday, 17 October, 2024 --------------------------------------------- Notes
- Associative arrays can also be defined like this
declare -A assocArr=( [key1]=val1 [key2]=val2 ) - By doing:
${!Locations[@]}we're looping through the keys, this is caused by adding!so that indirection occurs - Therefore
$Locationis a key of the associative array - Then we get the value from
"${Locations[$Location]}" - When you pass in the key to the array variable of an associative array it's value is returned
- The script could've been shorter but I wrote it this way so that it is easy to read and understand, might help others out
- Let me know if any questions
On another note you could use the function in the script and just pass in the timezone and location, if you decide you don't want to use an associative array anymore.