1

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?

Community
  • 1
  • 1
caeycae
  • 1,137
  • 3
  • 12
  • 28

2 Answers2

5

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).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
0

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278