ok, well it's still a bit circuitous, but at least it avoids creating temporary artifacts:
id="$(security find-identity -v | grep "2) " | cut -d '"' -f 2)"; code="$(echo $id | awk -F'()' '{print $2}')"; security find-certificate -c "$id" -p > $code.pem; certtool d $code.pem | grep -A1 "$id" | tail -1 | cut -d ':' -f 2
Selector can be parameterized as the index of the find-identity command, and output is just the team code by itself.
Topic:
Code Signing
SubTopic:
Certificates, Identifiers & Profiles