var="keychain-access-groups"
declare -a val=$( /usr/libexec/PlistBuddy -c "Print $var" "sample.plist")
echo ${val}
echo ${val[0]}
Ouput:
Array { ABCD.com.bus.NoEntitlements ABCD.com.bus.sharing }
Array { ABCD.com.bus.NoEntitlements ABCD.com.bus.sharing }
How to get the first item in the Array?
It seems
PlistBuddyproduces output like this:That is, multiple lines. If you want to get to the elements of the
Array, you need to first slice off the first and last lines:Next, to read this into a Bash array, you need to surround the
$(...)subshell with another(...), like this:After this, you can access the first value with
${val[0]}and the second value with${val[1]}.