Escape a path for sed search pattern [duplicate]

2019-02-28 02:36发布

Possible Duplicate:
sed, foward slash in quotes

In my bash script I have a path string, which I should use in sed pattern.

SRC_PATH="$PWD"
sed "s/<SRC_PATH>/$SRC_PATH/g" template.sh > replaced.sh

How can I escape the $SRC_PATH string so it would be safely accepted by sed as a literal replacement?

2条回答
smile是对你的礼貌
2楼-- · 2019-02-28 02:57

You need not escape it. Just use other delimiter:

sed "s@<SRC_PATH>@$SRC_PATH@g" template.sh > replaced.sh

But you must be sure that SRC_PATH contains no @ (or other symbol if you choose it).

查看更多
孤傲高冷的网名
3楼-- · 2019-02-28 03:06

Use s%$OLDPATH%$NEWPATH%. You can choose your delimiter. If % is too dangerous, consider Control-A instead.

查看更多
登录 后发表回答