This lines of a Bash (4.0) Scriptklang=([string_0]='element_0' [string_1]='element_1' [string_2]='element_2')echo ${klang[*]}     # should output the all set elementsecho ${#klang[*]}    # should output the number of set elementsreturns (for some reason):element_21There are no other commands set in this script and I am really new in Bash scripting. What happened to the other elements (Element_0 and Element_1)? This really worked perfectly with indexed arrays and numbers and everything, but doesn't work with text – well except for one element which is a bit less than I asked for… Where did I miss something?
Bash scripting: Associated Array only recognizes one value when several given
bash;array
If you don't declare the variable as an associative array, then theklang=([string_0]='element_0' [string_1]='element_1' [string_2]='element_2')is taken as a normal array assignment (though note that in bash and ksh93, normal arrays are not really normal arrays, they're more sparse arrays or associative arrays with keys limited to positive integers).In bash, for a normal array, keys are evaluated as arithmetic expressions. As an arithmetic expression, string_0 evaluates to the content of $string_0, and if that's empty, to 0. So the above is likely to be the same as:klang=([0]='element_0' [0]='element_1' [0]='element_2')So, that's the same as defining ${klang[0]} (which is the same as $klang) 3 times.That contrasts with ksh93 arrays (which bash mostly copied), where the a=([x]=y) syntax is only  supported for associative arrays and would automatically create an associative array when used.zsh associative arrays (which predate bash ones by decades) are different again. zsh normal arrays are normal arrays, zsh doesn't support  the bogus a=([x]=y) syntax. In zsh, you have to declare associative arrays before defining them, and they are defined as:normal_array=(val1 val2 val3)typset -A associative_arrayassociative_array=(key1 val1 key2 val2)associative_array+=(key3 val3...)And $associative_array expends to the non-empty values like for normal arrays (in undefined order), and $associative_array[@]} to all the values like for normal arrays. ${(k)associative_array} for the non-empty keys, ${(k@)associative_arrays} for all the keys, ${(kv@)associative_array} for the keys and values, so you can print the content of an associative array with:printf '%s => %s\n' ${(@kv)associative_array}That also means the copying an associative array is a lot less cumbersome than with ksh93/bash:typeset -A BB=(${(kv@)A})as opposed to:unset Btypeset -A Bfor k in ${!A[@]}; do B[$k]=${A[$k]}; done