1

I need to store in a unix variable a string that can contain special characters.

In my case I am decrypting a text that returns to me Her$p7 that I need to store. Obviously that result can be any string (example i*Fi+K'7).

Would they know how I can save that result that I throw in a variable and use it like this

var=Her$p7

echo var

by example?

I use this for retrieve the string

echo "`${FDL_HOME}/bin/cypher2 DEC ${CF_KEYPFX} ${CF_KEYLEN} ${CF_PASS}== | grep "decrypt text" | gawk -F': ' '{print $2}'`"|perl -e print
2
  • I do not understand the problem. var='Her$p7' should work. If you do not want that $ is handled specially, you need to escape or to put in single quotes. Commented Jun 6, 2018 at 13:33
  • The error is by the $. But this special character can be in any position and any character. The set or the export does not work. Commented Jun 6, 2018 at 13:35

1 Answer 1

5

The syntax is correct, if not robust, and the issue is in understanding string interpolation. In the standard string context, the dollar sign signifies to the shell that it's about to interpret a variable. Generally, this means to replace the variable with the value of the variable. Consider:

$ t1=Her$p7
$ t2="Her$p7"
$ t3='Her$p7'
$ t4="$(echo 'Her$p7')"
$ echo "t1: $t1; t2: $t2; t3: $t3, t4: $t4"
t1: Her; t2: Her; t3: Her$p7; t4: Her$p7

Note that while setting t1 (the first line) and t2, $p7 was interpreted as a variable (which you had not set), and thus was consequently replaced with it's value (empty/nothing). So, t1 and t2 were set to the value Her<nothing> -> Her.

In the third case, we used single quotes to tell the shell "no interpolation please; I mean strictly what I say". So, t3 is set to exactly the string you typed.

In the last case, we use the subshell operator ($( ... )) to set the variable t4 to the output of subshell command. In this case, we use double quotes to make sure we capture the entire output, but because we aren't typing the variable $p7, the shell won't interpolate the output of the command.

So, you should be good to go with something like:

$ yourVar=$(echo "`${FDL_HOME}/bin/cypher2 DEC ${CF_KEYPFX} ${CF_KEYLEN} ${CF_PASS}== | grep "decrypt text" | gawk -F': ' '{print $2}'`" | perl -e print)

Moving into the last decade, we could clean that up slightly by not using backticks for subshell operations:

$ yourVar=$(echo "$(${FDL_HOME}/bin/cypher2 DEC ${CF_KEYPFX} ${CF_KEYLEN} ${CF_PASS}== | grep "decrypt text" | gawk -F': ' '{print $2}')" | perl -e print)
Sign up to request clarification or add additional context in comments.

5 Comments

When i try this: pass="$(echo "${FDL_HOME}/bin/cypher2 DEC ${CF_KEYPFX} ${CF_KEYLEN} ${CF_PASS}== | grep "decrypt text" | gawk -F': ' '{print $2}'"|perl -e print)" I have this error: line 65: echo Her$pne7|perl -e print: command not found
That implies a syntax error; likely a quoting issue. Build up your command piece by piece (e.g., run cypher2 and read the output, run cypher2 | grep ... and read the output. For example, this works for me: echo "$(echo "decrypt text: Her\$pne7" | grep "decrypt text" | gawk -F': ' '{print $2}')"
I run this: echo "${FDL_HOME}/bin/cypher2 DEC ${CF_KEYPFX} ${CF_KEYLEN} ${CF_PASS}== | grep "decrypt text" | gawk -F': ' '{print $2}'"|perl -e print and print is Her$pne7 When i try to do this yourVar=$(echo "${FDL_HOME}/bin/cypher2 DEC ${CF_KEYPFX} ${CF_KEYLEN} ${CF_PASS}== | grep "decrypt text" | gawk -F': ' '{print $2}'"|perl -e print) The var comes empty :(
What value does perl -e print offer here?
You are big hunteke. I deleted perl -e print and the problem is gone. I can store the variable with the character or special characters that it returns and use it without incident. Thanks a lot friend.

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.