6:36 PM
I don't understand it myself so far :(
But, the OP of that question somehow found, I am guessing, that putting two backslashes at the end of the string allowed them to get one printed in the middle of the string...
they could have just quoted their sed
command properly, but they found another, strange way of doing it
They wanted Hello \ World
which can be easily made like that:
$ echo '' > file
$ sed 'cHello \\ World' file
Hello \ World
but cannot be made like this
$ sed "cHello \\ World" file
Hello World
I think I understand that... in double quotes, backslashes escape the next character. The first backslash gets taken away by the shell. Sed doesn't print the one backslash it saw, because it also treats a backslash as an escape (but spaces don't need to be escaped - they are not special in Sed (are they?))
however, strangely, this does work:
$ sed "cHello \\ World\\" file
Hello \ World
So, OP apparently found something interesting
I don't know why that happens.
What does the last backslash signify?
This only seems to work with the a
c
and i
commands
whatever comes after those commands in general gets printed. But it seems there are some exceptions. For one thing, to make it more readable, you conventionally put a backslash before the line you want to insert...
Sed only complains about the missing backslash if there are no non-whitespace characters after the command:
zanna@toaster:~/playground$ sed "c" file
sed: -e expression #1, char 1: expected \ after `a', `c' or `i'
zanna@toaster:~/playground$ sed "c " file
sed: -e expression #1, char 2: expected \ after `a', `c' or `i'
OP's magic doesn't work, if there are no non-whitespace characters before the backslash they want to print, unless there's a backslash after the sed command...
zanna@toaster:~/playground$ sed "c\\\\" file
zanna@toaster:~/playground$ sed "c\ \\\\" file
\
zanna@toaster:~/playground$ sed "c.\\\\" file
.\
so, after one of these commands, when generally everything you write gets printed, backslashes are still doing special things I don't understand
they don't even have to be at the end!
zanna@toaster:~/playground$ sed "c.\\more\\" file
.\more
zanna@toaster:~/playground$ sed "c.\\\\more" file
.\more
I suspect those two work for different reasons... in the first case, OP's voodoo. In the second, sed
saw two backslashes so printed one, as with 'c.\\more'
@Zanna same thing there, that's not weird, is it? four backslashes in double quotes = 2 backslashes in sed = 1 backslash to stdout
but if you put something in between the two sets of backslashes you get the same result... so it's as if the backslashes in the middle get quoted by the implicit or explicit one at the beginning, and the explicit one at the end