Elliot Rodriguez Elliot Rodriguez - 9 months ago 32
Bash Question

Escaping parentheses when sending GPG passphrase to a Perl script

I am having some difficulty with a Perl script that is invoked from cron.

One of the arguments for the script is a GPG passphrase. This is later interpolated into a string that is sent to the shell.

This particular passphrase contains an open parentheses, and the script fails with a "syntax error near unexpected token `(" error.

This is the offending part of the phrase:


I have tried single and double quoting it before the value is used in the script, but that does not have an effect.

The phrase works correctly when invoking GPG directly from the shell, just not when it gets interpolated into the following:

`gpg --passphrase $gpgpp --batch -o $gpgofile -d $file`;

Where $gpgpp is the passphrase variable.

What is the correct way to escape this, and other potentially problematic characters?


The \Q and \E escape sequences are used to escape all "special" characters between them.

`gpg --passphrase \Q$gpgpp\E --batch -o $gpgofile -d $file`;

This should be done any time you have a variable that may contain characters which need to be escaped.