sed双引号与单引号

sed双引号与单引号,sed,Sed,我正在尝试替换某些包含符号$的文本: echo“\$hello=false”| sed-re“s/(\$hello=)false/\1true/”>$hello=false echo“\$hello=false”sed-re的/(\$hello=)false/\1true/”$hello=true 为什么单引号版本有效,而双引号版本无效?这是因为在双引号中,反斜杠\是用于转义的字符,因此shell在转义时将其删除。因此,它不会传递给sed。 尝试: “\$”=$,“\$”=\$看哦,我明白了

我正在尝试替换某些包含符号
$
的文本:

  • echo“\$hello=false”| sed-re“s/(\$hello=)false/\1true/”
    >$hello=false

  • echo“\$hello=false”sed-re的/(\$hello=)false/\1true/”
    $hello=true


为什么单引号版本有效,而双引号版本无效?

这是因为在双引号中,反斜杠
\
是用于转义的字符,因此shell在转义时将其删除。因此,它不会传递给sed。 尝试:


“\$”
=
$
“\$”
=
\$
看哦,我明白了。因此,如果我想使用双引号版本,我必须使用3个反斜杠,如So
echo“s/(\\\$hello=)false/\1true/”
。谢谢
echo "s/(\$hello = )false/\1true/"
echo 's/(\$hello = )false/\1true/'