Why does sed require 3 backslashes for a regular backslash?

后端 未结 7 1627
余生分开走
余生分开走 2020-11-29 01:26

I\'m curious, why does sed need 3 \\ just to recognize one? I\'d understand it needing 2, but 3 I don\'t.

EDIT: here\'s an example on my Windows compute

7条回答
  •  迷失自我
    2020-11-29 02:11

    I was able to reproduce this behavior using Vista and Cygwin 1.7.0.

    • Two backslashes produce the error
    • either three or four backslashes work
    • Five gives the same error

    Two backslashes become a single backslash in the shell which then in sed escapes the forward slash which is the middle delimiter.

    \\/ -> \/ (which makes the forward slash a regular character instead of a delimiter)
    

    Three of them: The first two become one in the shell which then escape the third one in sed

    \\\/ -> \\/
    

    Four: Each pair become single ones in the shell then the first resulting one escapes the second in sed

    \\\\/ -> \\/ 
    

    Edit:

    Oh, I forgot to say that both single quotes and double quotes worked the same for me (cmd.exe doesn't make the distinction that Bash, et al, makes).

提交回复
热议问题