bash 在unix变量中设置带有特殊字符的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50721741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:56:12  来源:igfitidea点击:

Set string with special character in unix variable

bashshellunix

提问by alemarchan

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

我需要在 unix 变量中存储一个可以包含特殊字符的字符串。

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

在我的情况下,我正在解密一个文本,该文本返回给我需要存储的Her$p7。显然,结果可以是任何字符串(例如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

var=她$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 }'`"|perl -e print

回答by hunteke

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:

语法是正确的,如果不是健壮的话,问题在于理解字符串插值。在标准字符串上下文中,美元符号向 shell 表示它即将解释一个变量。通常,这意味着将变量替换为变量的值。考虑:

$ 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, $p7was interpreted as a variable (which you had not set), and thus was consequently replaced with it's value (empty/nothing). So, t1and t2were set to the value Her<nothing>-> Her.

请注意,虽然设置t1(第一行)和t2,$p7被解释为一个变量(您没有设置),因此被替换为它的值(空/无)。所以,t1t2被设定的值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, t3is set to exactly the string you typed.

在第三种情况下,我们使用单引号告诉 shell “请不要插值;我的意思是我所说的”。因此,t3设置为您键入的字符串。

In the last case, we use the subshell operator ($( ... )) to set the variable t4to 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.

在最后一种情况下,我们使用 subshel​​l 运算符 ( $( ... )) 将变量设置t4为 subshel​​l 命令的输出。在这种情况下,我们使用双引号来确保我们捕获了整个输出,但是因为我们没有输入变量$p7,所以 shell 不会插入命令的输出。

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 }'`" | perl -e print)

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

进入过去十年,我们可以通过使用反引号进行子shell操作来稍微清理一下:

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