# why is it true that three backslashes are needed on windows for sed replace

Refering to this question:

Why is an extra `\`needed in `cmd.exe` to work with `sed` (MinGW msys-1.0) when `\` is not a special character according to `cmd /?` (see last paragraph or here)?

The following special characters require quotation marks: & < > [ ] { } ^ = ; ! ' + , ` ~ [white space]

First backslash escapes the second, robbing him his special meaning. Two remaining backslashes are given to `sed` which escapes the third by using the second, so one verbatim single backslash is left in the end, which is matched by my search and replace. But still I'm unsatisfied with this explanation, because:

cmd does not perform tokenizing so the first step of escaping doesn't make sense ... from here, `\` has only some special meaning when preceding `"`...so what is the real explanation?

on bash in linux:

``````echo 'sample\input' | sed 's/\\/----/'
sample----input
``````

on `cmd.exe` in windows xp sp3 (no `'` needed):

``````echo sample\input | sed "s/\\/----/"
sed: -e expression #1, char 9: unterminated 's' command
// for some reason sed received only one backslash which causes him trouble ?

echo sample\input | sed "s/\\\/----/"
sample----input
``````
-

## migrated from stackoverflow.comFeb 14 '13 at 8:35

This question came from our site for professional and enthusiast programmers.

Sed is doing it, it uses a regex in the "find" section. it uses BRE or ERE or PCRE depending on the switch. Backslash is special within a regex.

I haven't used your version of using single quotes 'cos that makes no sense to me in cmd.exe!! cmd.exe uses double quotes if at all.

And it works fine.

tested with gnuwin32's sed run from cmd.exe as it is meant to be.

``````C:\>echo sample\input | sed "s/\\/----/"
sample----input

C:\>sed --v
GNU sed version 4.2.1
Copyright (C) 2009 Free Software Foundation, Inc.
``````

If I was testing cygwin's sed i'd run it from the cygwin window as that's where cygwin programs are meant to be run. And then i'd use single quotes. msys seems similar to cygwin in that sense.

-
I notice my answer only partially explains things, as it still doesn't explain the three. Why not two –  barlop Mar 6 '13 at 23:10
the "windows" sed or "unix" sed or both? –  panny Mar 6 '13 at 23:11
@panny don't know what he's using, or why he's using single quotes in cmd. But I tried a version of his one, with double quotes, and it has worked fine, just two backslahes. AH he's using msys apparently. –  barlop Mar 6 '13 at 23:15
If using msys, i'd suggest using mingw32 gui which I see done in screenshots of it. If you want to use cmd.exe then use a win32 port. Gnuwin32 has a windows port of sed. –  barlop Mar 6 '13 at 23:18
@panny I tend not to mix things. So, Cygwin things I use within cygwin's window in windows. If I was using msys i'd use it within a mingw32 window. And gnuwin32 stuff i'd use within cmd.exe But if you want to mix things i'd try using double quotes in cmd as you're meant to in cmd. But mixing things seems unnecessary and may complicate things –  barlop Mar 6 '13 at 23:21