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?

0

1 Answer 1

1

You need to escape both the shell, and sed:

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

Not the answer you're looking for? Browse other questions tagged .