Tried to use bash arrays but for some reason this fails? Im trying to make a script that should be executed with crontab, but I'm having difficulties to declare an array an fetch the key and value from that array. (Running MacOS 10.12.6)

This is what i'v got so far:

#!/bin/bash

declare -A listofnames

listofnames[one]="one test"
listofnames[two]="two test"

for val in "${listofnames[@]}"; do
    echo $val
done


And this throws me an error: declare: usage: declare [-afFirtx] [-p] [name[=value] ...] and the output it two test.

Expected output:

$ ./test.sh
one test
two test
It's not required that you declare your array upfront, but if you do, you need to use the lower case "a"; not the capital.

declare -a listofnames


Secondly, you need to use numerical indicies when referencing an array element

listofnames[one] &lt;----- incorrect
listofnames[1]   &lt;----- correct


So, for your two elements, it should look like this:

listofnames[1]="one test"
listofnames[2]="two test"


Or you assign all values at once, taking care of the first element having index 0

listofnames=("", "one test", "two test)