问题描述
我正在使用sed在unix shell(bash)上的一个字符串中转义('\\')分号(';')。当我直接执行它而不将值赋值给变量时,它将起作用。也就是说,
$ echohello | sed的/ \([^ \\] \); / \1\\; / g'
hello\;
$
但是,当上述命令分配给一个变量:
$ result =`echohello; | sed的/ \([^ \\] \); / \1\\; / g'`
$
$ echo $ result
hello;
$
任何想法为什么?
我尝试使用带有和没有引号的值,但没有帮助。任何线索非常感谢。
btw,我第一次认为字符串末尾的分号以某种方式作为终结符,因此shell没有继续执行 sed
(如果有任何意义)。但是,这并不是一个问题。我尝试使用分号不在字符串的末尾(某处之间)。我仍然看到与以前相同的结果。也就是说,
$ echohel; lo| sed的/ \([^ \\] \); / \1\\; / g'
hel\; lo
$
$ result =`echo'hel; lo| sed的/ \([^ \\] \); / \1\\; / g'`
$
$ echo $ result
hel; lo
$
我觉得有趣使用反向滴答提供一个结果(您的结果),并使用 $(...)
提供另一个结果(想要的结果):
$ echohello; | sed的/ \([^ \\] \); / \1\\; / g'
hello\;
$ z1 = $(echohello;| sed's / \([^ \\] \); / \1\\; / g')
$ z2 =`echo'hello; | sed的/ \([^ \\] \); / \1\\; / g'`
$ printf%s\\\
$ z1$ z2
hello\;
hello;
$
如果您需要使用现代的 x = $(...)
符号代表旧的 x =`...`
符号,这可能是。外壳使用后面的勾号进行了额外的反斜杠解释。调试shell脚本调用 al
(参数列表)时,我可以使用一个小程序来演示这个。您可以使用 printf%s\\\
进行模拟:
$ z2 =`echo'hello; | al sed's / \([^ \\] \); / \1\\; / g'`
$ echo$ z2
sed
s / \([^ \] \); / \1\; / g
$ z1 = $(echohello;| al sed's / \([^ \ \] \); / \1\\; / g')
$ echo$ z1
sed
s / \([^ \\] \); / \1\\; / g
$ z1 = $(echohello;| printf%s\\\
sed's / \([^ \\\ \\] \); / \1\\; / g')
$ echo$ z1
sed
s / \([^ \\] \\); / \1\\; / g
$
可以看到,由 sed
执行的脚本根据您是否使用 x = $(...)
符号而不同, x =`...`
符号。
s / \ ([^ \] \); / \1\; / g#``
s / \([^ \\] \); / \1\\; / g#$()
摘要
使用 $(...)
;这更容易理解。
I'm trying to escape ('\') a semicolon (';') in a string on unix shell (bash) with sed. It works when I do it directly without assigning the value to a variable. That is,
$ echo "hello;" | sed 's/\([^\\]\);/\1\\;/g'
hello\;
$
However, it doesn't appear to work when the above command is assigned to a variable:
$ result=`echo "hello;" | sed 's/\([^\\]\);/\1\\;/g'`
$
$ echo $result
hello;
$
Any idea why?
I tried by using the value enclosed with and without quotes but that didn't help. Any clue greatly appreciated.
btw, I first thought the semicolon at the end of the string was somehow acting as a terminator and hence the shell didn't continue executing the sed
(if that made any sense). However, that doesn't appear to be an issue. I tried by using the semicolon not at the end of the string (somewhere in between). I still see the same result as before. That is,
$ echo "hel;lo" | sed 's/\([^\\]\);/\1\\;/g'
hel\;lo
$
$ result=`echo "hel;lo" | sed 's/\([^\\]\);/\1\\;/g'`
$
$ echo $result
hel;lo
$
I find it interesting that the use of back-ticks gives one result (your result) and the use of $(...)
gives another result (the wanted result):
$ echo "hello;" | sed 's/\([^\\]\);/\1\\;/g'
hello\;
$ z1=$(echo "hello;" | sed 's/\([^\\]\);/\1\\;/g')
$ z2=`echo "hello;" | sed 's/\([^\\]\);/\1\\;/g'`
$ printf "%s\n" "$z1" "$z2"
hello\;
hello;
$
If ever you needed an argument for using the modern x=$(...)
notation in preference to the older x=`...`
notation, this is probably it. The shell does an extra round of backslash interpretation with the back-ticks. I can demonstrate this with a little program I use when debugging shell scripts called al
(for 'argument list'); you can simulate it with printf "%s\n"
:
$ z2=`echo "hello;" | al sed 's/\([^\\]\);/\1\\;/g'`
$ echo "$z2"
sed
s/\([^\]\);/\1\;/g
$ z1=$(echo "hello;" | al sed 's/\([^\\]\);/\1\\;/g')
$ echo "$z1"
sed
s/\([^\\]\);/\1\\;/g
$ z1=$(echo "hello;" | printf "%s\n" sed 's/\([^\\]\);/\1\\;/g')
$ echo "$z1"
sed
s/\([^\\]\);/\1\\;/g
$
As you can see, the script executed by sed
differs depending on whether you use x=$(...)
notation or x=`...`
notation.
s/\([^\]\);/\1\;/g # ``
s/\([^\\]\);/\1\\;/g # $()
Summary
Use $(...)
; it is easier to understand.
这篇关于为bash中的变量分配一个分号(';')的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!