Skip to content

Instantly share code, notes, and snippets.

@virtualstaticvoid
Created January 28, 2026 09:50
Show Gist options
  • Select an option

  • Save virtualstaticvoid/87a2f55340615ca034c62ce1fe22f47e to your computer and use it in GitHub Desktop.

Select an option

Save virtualstaticvoid/87a2f55340615ca034c62ce1fe22f47e to your computer and use it in GitHub Desktop.
Create a GPG key with name and email
#!/bin/bash
set -e
set -x
email=$1
name=$2
usage() {
echo "Usage: $0 <email> <name>"
}
if [ -z "${email}" ]; then
echo "Error: Email address is required."
usage
exit 1
fi
if [ -z "${name}" ]; then
echo "Error: Full name is required."
usage
exit 1
fi
tmpcfg=$(mktemp)
cat <<EOF > $tmpcfg
Key-Type: 1
Key-Length: 4096
Name-Real: ${name}
Name-Email: ${email}
Expire-Date: 0
%no-protection
%commit
%echo Done
EOF
key_id=$(
gpg --batch --full-generate-key --status-fd 1 "${tmpcfg}" \
| awk '/^\[GNUPG:\] KEY_CREATED/ { print $4 }'
)
gpg --batch --armor --export $key_id > gpg-public-key.pem
gpg --list-packets < gpg-public-key.pem | awk '$1=="keyid:"{print$2}' | head -n 1 > gpg-key-id
rm -f "${tmpcfg}"
printf "\nGenerated GPG key: "
cat gpg-key-id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment