Madhu Avinash Madhu Avinash - 9 days ago 9
Linux Question

Get Array of Values using plistBuddy

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?

Answer

With GNU sed:

I suggest to replace

/usr/libexec/PlistBuddy -c "Print $var" "sample.plist"

by

/usr/libexec/PlistBuddy -c "Print $var" "sample.plist" | sed -r 's/^.* \{ (.*) \}.*/\1/'

to remove Array { and } from every line.