That's due to sh
's double-quoted string parsing rule.
Posix specifies how sh
parses double-quoted strings.
The backslash shall retain its special meaning as an escape character (see Escape Character (Backslash)) only when followed by one of the following characters when considered special:
$ ` " \
In other words, sh
lefts the backslash which is followed by characters other than $
'
"
\
.
So, if sh
meets the double-quoted string sed "s/\\\/\//"
, sh
parses it as follows.
- The first two
\\
is changed into \
. Because the first \
is followed by the second \
.
- The third and fourth
\
is still left in the string. Because both of them are followed by /
, which is not special in double-quoted string.
After pasring, sh
passes the string s/\\/\//
to sed
, which substitutes the first occurence of \
into /
.
With same reasoning, when sh
meets the string, "sed s/\\\\/\//"
, sh
passes /\\/\//
to sed
, which also substitutes the first occurence of \
into /
.