You can use
gpg --dry-run to prevent changes.
Following line will print the key id in its output (can be modified using the usual modifiers like
--with-colons for further processing).
A4FF2279 is the key ID in here.
$ gpg --dry-run --import pubkey.asc
gpg: key A4FF2279: public key "[User ID not found]" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
gpg: no ultimately trusted keys found
Just tried it, the key did not get stored to my keychain, but the key ID was printed. But watch out with
--dry-run, the man page has a warning:
Don't make any changes (this is not completely implemented).
A more in-depth discussion of multiple variants for human-readable, machine-readable and very technical output for different versions of GnuPG is found in the Stack Overflow question How to display gpg key details without importing it?. All of them will also present the key ID.