Dollar symbol in Shell Script Variable

Hi,

I am working on PGP encryption. I am getting public keys from some file.
One of the key has dollar sign in it "$" Example: "abc$123"

echo 'passphrase='$passphrase  --> Giving correct value abc$123

But if I use $passphrase in PGP command getting Invalid passphrase error.
If I hardcode "abc$123" encryption is working fine.
Getting error only if I am referring $passphrase in PGP command.

Please help how to handle $ sign in shell script variable

Thanks,
Sreehari

I can't tell why code I can't see isn't working. Please show your code.

I doubt that the dollar sign causes the problem, though. Shell is quite careful not to interpret dollar signs in strings as variables -- only in the shell's code itself is the value special.

Variables inside single quotes don't work, variables inside double quotes are expanded. This might be the issue.

$ echo '$asdf'

$asdf

$ echo "$asdf"

variablecontents

$

Single tics or an escape (backslash) stops the shell from translating a variable because it takes the $ literally rather than as a metcharacter.

password="123\$abc"
# and echo works

echo "$password

Is that what you mean?

Working code: Hardcoded passphrase

pgp -es $SourceFile --Recipient $recipient  --signer $signer --passphrase "abc$123" --output $EncryptFile

Not Working:

echo 'passphrase='$passphrase  --> gives correct value abc$123

pgp -es $SourceFile --Recipient $recipient  --signer $signer --passphrase $passphrase --output $EncryptFile     ####Gives invalid passphrase 

Thanks,
Sreehari

If you don't set passphrase to anything, $passphrase will not work.

If you did set passphrase to something, there may be something wrong with the way you did it. I can't tell because you didn't show that code. Show all your code.

You should probably quote it, too, "$passphrase", to prevent the shell from splitting it on spaces etc.

Hi,

I am maintaining all keys in a file say ABC.pwdf

Contents of ABC.pwdf

<bank> <recipient> <signer> <passphrase>
WFB 0X12343 0x1234 abc$123

================

Reading these keys in shell script

lnCount=0;
while read pString
do
	lvbank=`echo  ${pString}|cut -d ' ' -f1`

	if [ "$lvbank" = "$l_bank" ]``
	then		
		lnCount=`expr $lnCount + 1`
		recipient=`echo  ${pString} | cut -d ' ' -f2`;
		signer=`echo  ${pString} | cut -d ' ' -f3`;
		passphrase=`echo  ${pString} | cut -d ' ' -f4`;		
	fi;
done < ${PwdFileName};

======================
I am getting all values correctly. Having problem only when I am using passphrase in PGP command

Thanks,
Sreehari

Are you sure you're running pgp with the correct passphrase when using the "hardcoded" one? With what you showed in post#4, wouldn't it look like

{ read; while read lvbank recipient signer passphrase REST; do echo pgp -es $SourceFile --Recipient $recipient  --signer $signer --passphrase "abc$123" --output $EncryptFile; done; } < file
pgp -es --Recipient 0X12343 --signer 0x1234 --passphrase abc23 --output

because it expands the $1 positional parameter (which is empty) as opposed to

{ read; while read lvbank recipient signer passphrase REST; do echo pgp -es $SourceFile --Recipient $recipient  --signer $signer --passphrase $passphrase --output $EncryptFile; done; } < file
pgp -es --Recipient 0X12343 --signer 0x1234 --passphrase abc$123 --output

See the difference?

1 Like

What we want to say is that --passphrase "abc$123" does NOT work as intended because it expands to --passphrase "abc${1}23" and if $1 is empty expands to --passphrase "abc23" .
But

passphrase='abc$123'
... --passphrase "$passphrase"

works correctly. Maybe it does not work because your actual passphrase is abc23 ?

For the third time, that is not all of your code. Now the bit with PGP in it is missing.

Post all your code.

Thanks RudiC, I got the difference between harcoded value and variable. We decided to change passphrase without dollar sign.

If your code - whatever it is - is evaluating dollar signs in passwords, that's a big problem. What if someone set their password to $(rm -Rf /var/importantdata) ?

Please show your code so we can find out why it's doing that.