0

I have a variable in a bash script with a length of 64 characters

authkey=$(LC_ALL=C tr -cd 'a-zA-Z0-9,;.:_#*+~!@$%&()=?{[]}|><-' < /dev/random | head -c 64)

if i parse the variable to perl to do a string substitution

perl -pi -e "s/'AUTH_KEY',         'put your unique phrase here'/'AUTH_KEY',         '$authkey'/" test.txt

depending on the selected random characters the length of the output differs.The output looks the following (The first string is the output in the resulting text file, the second string is the echo'ed output of the variable in the bash script)

q=dB7oUz59.IDBXI:i>ckW4oy3smX&k:-C.[rIf*9w}H=(N93yiB&nk{fP:y0_
q=dB7oUz59.IDBXI:i>ckW4oy3smX&k$s:-C.[rIf*9w}H=(N93yiB&nk{fP:y0_

5A+BwP~l3~<evp.ciTkMYtvmPjyMrL=):Qj1VaMI(,TSS,ZGMcd.m,4W
5A+BwP~l3~<evp.ciTkMYtvmPjyMrL=):Qj1VaMI(@Dk7UNgs,TSS,ZGMcd.m,4W

dX73}i5G1d;L*J=60WHHe<!61Ji_KJ)T5B~b2bCfaNDjBQr_N]}3HS=;GzAaX<gB
dX73}i5G1d;L*J=60WHHe<!61Ji_KJ)T5B~b2bCfaNDjBQr_N]}3HS=;GzAaX<gB

6Ndn(9+:>(6>*rh?B.m),3POp)>sfm8c1rh9vXr~fzZj;]!)kf3#60=M
6Ndn(9+:>(6>*rh?B.m),3POp)>sfm8c1rh9vXr~fzZj;]@YH!)kf3#$=$$ckt=M

FYMI,K|6WutC&dr-3]6)f(>QU-~{vBX>n!J-zq:kK84T|fZ7UW:{1&qU[nwYZLmC
FYMI,K|6WutC&dr-3]6)f(>QU-~{vBX>n!J-zq:kK84T|fZ7UW:{1&qU[nwYZLmC

5A+BwP~l3~<evp.ciTkMYtvmPjyMrL=):Qj1VaMI(,TSS,ZGMcd.m,4W
5A+BwP~l3~<evp.ciTkMYtvmPjyMrL=):Qj1VaMI(@Dk7UNgs,TSS,ZGMcd.m,4W

v1FR8c8}dZD(QGwOrr%M{FSUw*?h.JGI?Ay4tgRVp~l7C5eAxW<w<;c}emeX#S
v1FR8c8}dZD(QGwOrr%M{FSUw*?h.JGI?Ay4tgRVp@s~l7C5eAxW<w<;c}emeX#S

+MGg0=*NrhJ}.qPkk6v[lc)J.uiW1o?LL5t<HTC#Q-hSeqn%-ke!cwL5tk[e
+MGg$|=*NrhJ}.qPkk6v[lc)J.uiW1o?L$55L5t<HTC#Q-hSeqn%-ke!cwL5tk[e

each character dropout was caused by either a $ or @ at the beginning of the group of characters. Is there a way to prevent that behaviour? Best regards Ralf

1
  • Perl performs variable interpolation in the replacement string, and $ and @ are variable prefixes. See stackoverflow.com/questions/3694851/… for a related issue. Commented May 23, 2014 at 23:58

2 Answers 2

1

Using a single quote ' as the delimiter instead of slash / for the substitution suppresses variable interpolation

$ foobar=\$bar; perl -p -e "s'foo'$foobar'"
xx
xx
foo
$bar
^C
$ 

Unfortunately the single quotes that are matched in the substitution now need escaping

foobar=\$bar; perl -p -e "s'\'foo'$foobar\''"
x
x
'foo
$bar'
^C

But that seems to get passed through to Perl OK, without munging the authkey contents with sed

Sign up to request clarification or add additional context in comments.

Comments

0

You can escape $ and @ before calling perl:

 authey=$(echo -n "$authkey" | sed -re 's/(\$|\@)/\\\1/g')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.