2

I use a string that contains \ in a sed expression and I want to keep it in the output of sed

$ A=w
$ B="\ "
$ echo word | sed "s/$A/$B/"
   ord

I want to obtain \ ord instead of ord.

What is the most elegant way of doing it?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
user123456
  • 5,018

1 Answers1

1

You need to escape both the shell, and sed:

$ A=w
$ B="\\\ "
$ echo word | sed "s/$A/$B/"
\ ord
heemayl
  • 56,300