$ echo 'output: " ' | sed "s/\"/\"/"
output: "
$ echo 'output: " ' | sed "s/\"/\\\"/"
output: "
$ echo 'output: " ' | sed "s/\"/\\\\\"/"
output: \"
I want to understand what is happening in the first and second example.
What I understand is, since I am using double quotes for the sed
expression, \"
is interpreded as "
and \\
is interpreted as \
.
If so then why is my second output comming out to be "
instead of \"
?
I know I could have used single quotes in sed
as 's/"/\"/'
and it doesn't works as inteded and replaces "
with "
.
I am really curious why this behaviour is observed for double quotes.
- GNU bash, version 5.1.4(1)-release (x86_64-pc-linux-gnu)
- sed (GNU sed) 4.8
's/"/\"/'
works? It shouldn't. You would need\\"
in the replacement text.