How to escape single quotes within single quoted strings

前端 未结 23 2201
说谎
说谎 2020-11-21 06:20

Let\'s say, you have a Bash alias like:

alias rxvt=\'urxvt\'

which works fine.

However:



        
23条回答
  •  不思量自难忘°
    2020-11-21 07:01

    Both versions are working, either with concatenation by using the escaped single quote character (\'), or with concatenation by enclosing the single quote character within double quotes ("'").

    The author of the question did not notice that there was an extra single quote (') at the end of his last escaping attempt:

    alias rxvt='urxvt -fg'\''#111111'\'' -bg '\''#111111'\''
               │         │┊┊|       │┊┊│     │┊┊│       │┊┊│
               └─STRING──┘┊┊└─STRIN─┘┊┊└─STR─┘┊┊└─STRIN─┘┊┊│
                          ┊┊         ┊┊       ┊┊         ┊┊│
                          ┊┊         ┊┊       ┊┊         ┊┊│
                          └┴─────────┴┴───┰───┴┴─────────┴┘│
                              All escaped single quotes    │
                                                           │
                                                           ?
    

    As you can see in the previous nice piece of ASCII/Unicode art, the last escaped single quote (\') is followed by an unnecessary single quote ('). Using a syntax-highlighter like the one present in Notepad++ can prove very helpful.

    The same is true for another example like the following one:

    alias rc='sed '"'"':a;N;$!ba;s/\n/, /g'"'"
    alias rc='sed '\'':a;N;$!ba;s/\n/, /g'\'
    

    These two beautiful instances of aliases show in a very intricate and obfuscated way how a file can be lined down. That is, from a file with a lot of lines you get only one line with commas and spaces between the contents of the previous lines. In order to make sense of the previous comment, the following is an example:

    $ cat Little_Commas.TXT
    201737194
    201802699
    201835214
    
    $ rc Little_Commas.TXT
    201737194, 201802699, 201835214
    

提交回复
热议问题