我正在编写一个shell脚本来创建一些配置文件.我必须替换文件中的一些模式,并且应该用路径替换一个模式.
所以我的问题是:有没有办法用路径替换我的占位符webpath而不在斜杠前面设置反斜杠?
也许有另一种工具可以做到这一点?
#!/bin/bash
# Let's say now, we are working in my $HOME directory
# Content of testfile (originally)
# 123456
# ABCDEF
# /home/superman
string="ABCDEF"
myfile="$HOME/testfile"
# test-1, this is okay
sed -i "/$string/d" $myfile
echo $string >> $myfile
# test-2, this fails
# ERROR (sed: -e expression #1, char 4: extra characters after command)
sed -i "/$PWD/d" $myfile
echo $PWD >> $myfile
# Not working either
sed -i ":$PWD:d" $myfile
echo $PWD >> $myfile
Run Code Online (Sandbox Code Playgroud)
我的问题:如何处理$ PWD情况?