How to escape single quotes within single quoted strings

前端 未结 23 2577
说谎
说谎 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 06:43

    Here is an elaboration on The One True Answer referenced above:

    Sometimes I will be downloading using rsync over ssh and have to escape a filename with a ' in it TWICE! (OMG!) Once for bash and once for ssh. The same principle of alternating quotation delimiters is at work here.

    For example, let's say we want to get: Louis Theroux's LA Stories ...

    1. First you enclose Louis Theroux in single quotes for bash and double quotes for ssh: '"Louis Theroux"'
    2. Then you use single quotes to escape a double quote '"'
    3. The use double quotes to escape the apostrophe "'"
    4. Then repeat #2, using single quotes to escape a double quote '"'
    5. Then enclose LA Stories in single quotes for bash and double quotes for ssh: '"LA Stories"'

    And behold! You wind up with this:

    rsync -ave ssh '"Louis Theroux"''"'"'"'"''"s LA Stories"'
    

    which is an awful lot of work for one little ' -- but there you go

提交回复
热议问题