Bash 使用sed命令在文件中添加带反斜杠的字符串

Bash 使用sed命令在文件中添加带反斜杠的字符串,bash,shell,sed,vagrant,vagrantfile,Bash,Shell,Sed,Vagrant,Vagrantfile,我需要添加一个文本字符串: --with-mpm=event \ 至第138位的httpd.spec 我试过: sed -i '138i--with-mpm=event \\' /root/rpmbuild/SPECS/httpd.spec 在引导虚拟机期间,此代码在Vagrantfile的bash脚本中运行。但是,脚本返回错误 当我选中httpd.spec时,输出是一个缺少反斜杠的字符串: --with-mpm=event 不过,直接在虚拟机的外壳中运行它可以正常工作 如何使用sed修

我需要添加一个文本字符串:

--with-mpm=event \
至第138位的httpd.spec

我试过:

sed -i '138i--with-mpm=event \\' /root/rpmbuild/SPECS/httpd.spec
在引导虚拟机期间,此代码在
Vagrantfile
的bash脚本中运行。但是,脚本返回错误

当我选中
httpd.spec
时,输出是一个缺少反斜杠的字符串:

--with-mpm=event 
不过,直接在虚拟机的外壳中运行它可以正常工作

如何使用
sed
修复它


谢谢

处理反斜杠的经验法则是不断累积反斜杠,直到得到预期结果

在这种情况下,此处的文字反斜杠需要用四个反斜杠编码:

sed -i '138i--with-mpm=event \\\\' /root/rpmbuild/SPECS/httpd.spec

通常,试着把反斜杠加起来,直到你得到它们<代码>\\<代码>\\\\?谢谢,我在代码中添加了两个反斜杠,它解决了这个问题。这很有帮助。我能记下你的答案,就像是对的!