philipp
philipp

Reputation: 16485

gpg — decrypt only content directly to stdout

I have the file secret.txt.gpg which I would like to decrypt, such that the content is stored within a variable like that:

TXT=$(gpg --decrypt secret.txt.gpg)

But this way a lot of extra gpg: … lines are added, containing information about the key etc. like that:

gpg: encrypted with 4096-bit RSA key, ID xxxx, created xxxx
  "xx xx (xx) <[email protected]>"
gpg: Signature made xxx
gpg:                using RSA key xxx
…
Secret Message

By the way:

gpg -d secret.txt.gpg > out.txt

is just writing the content into the file.

How can I capture the content only, without writing it to a file?

Update

Even though @Roger's answer is better and explains the why, I could make it using this:

TXT=$(gpg --decrypt secret.txt.gpg > /dev/stdout)

Upvotes: 4

Views: 7487

Answers (1)

Roger
Roger

Reputation: 193

The GPG messages are written to STDERR, which is why piping STDOUT to a file omits those messages.

If the encrypted file is not signed you usually suppress the messages about encryption by providing the --quiet switch. If the file is signed you will still get messages about the signature. Even if you provide the --skip-verify you will still get a message gently informing you that signature verification was suppressed.

In order to suppress all those message I suggest you pipe STDERR to /dev/null, e.g.:

TXT=$(gpg --decrypt secret.txt.gpg 2>/dev/null)

Upvotes: 7

Related Questions