How to escape single quotes within single quoted strings

前端 未结 23 2190
说谎
说谎 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

    If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:

     alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'"
     #                     ^^^^^       ^^^^^     ^^^^^       ^^^^
     #                     12345       12345     12345       1234
    

    Explanation of how '"'"' is interpreted as just ':

    1. ' End first quotation which uses single quotes.
    2. " Start second quotation, using double-quotes.
    3. ' Quoted character.
    4. " End second quotation, using double-quotes.
    5. ' Start third quotation, using single quotes.

    If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word.

提交回复
热议问题