How to escape single quotes within single quoted strings

前端 未结 23 2189
说谎
说谎 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:57

    In the given example, simply used double quotes instead of single quotes as outer escape mechanism:

    alias rxvt="urxvt -fg '#111111' -bg '#111111'"
    

    This approach is suited for many cases where you just want to pass a fixed string to a command: Just check how the shell will interpret the double-quoted string through an echo, and escape characters with backslash if necessary.

    In the example, you'd see that double quotes are sufficient to protect the string:

    $ echo "urxvt -fg '#111111' -bg '#111111'"
    urxvt -fg '#111111' -bg '#111111'
    

提交回复
热议问题