gpg — decrypt only content directly to stout

Viewed 2857

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) <xx@xx.xx>"
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 Answer is better and explains the why, I could make it using this:

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

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)
Related