Get list of secret key ids

gnupg

How can I easily get a list of secret key IDs available to the current GnuPG user?

I want a hypothetical command gpg --foo which I can use for:

keyids=$( ( gpg --foo ) )
for keyid in "${keyids[@]}" ; do …

What is the gpg --foo command that will just get me the key IDs for my secret keys?

Best Answer

It takes several seconds to complete, but this works:

gpg --list-secret-keys --with-colons \
    2> /dev/null \
    | grep '^sec:' | cut --delimiter ':' --fields 5

Thanks to @dirkt and @grawity for pieces of this answer.

Related Question