Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gpg — decrypt only content directly to stdout

Tags:

bash

gnupg

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)
like image 484
philipp Avatar asked Oct 27 '25 04:10

philipp


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)
like image 131
Roger Avatar answered Oct 30 '25 01:10

Roger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!