We are writing a bash script which basically does the job of python dictionary. Below is the code siffet we are using and the expected output.
#!/bin/bash
declare -A serviceTag
serviceTag["source"]="ccr"
declare -A services
services+=( ["dataservice"]="latest" )
serviceTag+=( ["services"]=services )
echo "$serviceTag"
The expected output is
{"source":"ccr","services":{"datasetvice":"latest"}}
But what we are getting is
ccrservices
Can somebody help us in what mistake we are doing here and how can we achieve this using bash and its code?
Regards, Kanthu
services
is just the stringservices
- you need array expansion for that as well. – muru Mar 26 '20 at 11:23printf '{"%s":"%s","%s":"%s":{"%s":"%s"}}\n' "${!serviceTag[@]}" "${serviceTag[@]}" "${!services[@]}" "${services[@]}"
– Jetchisel Mar 26 '20 at 11:31$avar
instead of${avar[@]}
with an arrayavar
refers to its first entry, not to the whole array, and you are NOT getting that output (ccrservices
) from the code you have posted. But, as already said, bash's arrays are unidimensional, so that will not work, anyways. – Mar 27 '20 at 02:02