8

I have a script that basically needs to do the following:

#!/bin/bash 

GPG_PUBLIC_KEY=<<EOF
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.12 (GNU/Linux)
...
-----END PGP PUBLIC KEY BLOCK-----
EOF

gpg --encrypt --with-public-key "$GPG_PUBLIC_KEY" myfile.txt

Is there a way to do this without importing the GPG key to the keyring first?

5

If you don't want to use the user's keyring at all, point GPG to a temporary keyring.

tmp=
trap 'rm -rf "$tmp"' EXIT INT TERM HUP
tmp=$(mktemp -d)
export GNUPGHOME="$tmp"
gpg --import <<EOF
…
EOF
gpg -e -r … myfile.txt

If you want to use the user's keyring as well, set GNUPGHOME only during the import command and pass --keyring "$tmp/pubring.gpg" to the second gpg command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.