GPG Decrypt: Bad Session key / Variable Unset / Exclamation Mark

I am trying to decrypt a gpg file, but am continually getting the following error:

gpg: decryption failed: Bad session key

I realised afterwards that the gpg passphrase (which is set by a script) has an exclamation in it

passphrase="my!pass"

When setting via TTY, bash complains

-bash: !pass: event not found

When I echo the variable, it's unset.

user@host:~$ echo $passphrase

user@host:~$

So, the variable is unknown, and I assume that's why I can't decrypt my file.

I've tried the following as a password:

  • my!pass
  • my
  • !pass
  • 'my!pass'
  • -bash: !pass: event not found
  • bash: !pass: event not found
  • -bash:
  • bash:
  • event not found

Fairly sure I've just learned a very painful lesson here, but it's worth a shot.

Can anyone help with:

  1. What does bash do with an unset variable if you try to use it?
  2. How does a script deal with an unset variable?
  3. Is there anything else I can try in place of the password?
  4. Is there an ASCII / UTF whitespace code that I can try in place of the password?

Solution 1:

There are a few comments worth noting.

  1. "... the gpg passphrase (which is set by a script)". I take it you didn't write the script. Please post the script. Are there any instructions to use it?
  2. passphrase="my!pass". This doesn't mean "use ! as part of the string". !pass means "read history, and get the last command starting with pass". If there are no commmands in history starting with pass, the error bash: !pass: event not found is raised, as you are getting at the command line. That's why I guess you didn't write the script. It is conceived for a case when there is some command in history starting with pass.
  3. "When setting via TTY (and I guess you executed passphrase="my!pass"), bash complains... When I echo the variable, it's unset". As mentioned above, an error is raised. But there are two more things happening: 1) The command is not executed. I.e., you don't even get "my" (you might have expected to get "my" = "my" + empty string) as the value of passphrase, it keeps unset. 2) The command is not even added to the history.

As for some of your questions:

  1. What does bash do with an unset variable if you try to use it? bash typically replaces it by an empty string. But generally speaking, it depends on how it is used, it may raise errors, you may have a sentence that replaces its value by a default value in case it is not set, etc. There is no single answer for this.
  2. How does a script deal with an unset variable? Same as above.

I don't know about the rest of your problem, but this perhaps helps you understanding the issue and solving it.