How to escape single quotes within single quoted strings

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

    Since one cannot put single quotes within single quoted strings, the simplest and most readable option is to use a HEREDOC string

    command=$(cat <<'COMMAND'
    urxvt -fg '#111111' -bg '#111111'
    COMMAND
    )
    
    alias rxvt=$command
    

    In the code above, the HEREDOC is sent to the cat command and the output of that is assigned to a variable via the command substitution notation $(..)

    Putting a single quote around the HEREDOC is needed since it is within a $()

提交回复
热议问题