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,067
  • 3
  • 12
  • 27

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
  • 698,132
  • 130
  • 858
  • 1,229
Igor Chubin
  • 57,130
  • 8
  • 114
  • 135
0

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

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229