How to escape single quotes within single quoted strings

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

    Another way to fix the problem of too many layers of nested quotation:

    You are trying to cram too much into too tiny a space, so use a bash function.

    The problem is you are trying to have too many levels of nesting, and the basic alias technology is not powerful enough to accommodate. Use a bash function like this to make it so the single, double quotes back ticks and passed in parameters are all handled normally as we would expect:

    lets_do_some_stuff() {
        tmp=$1                       #keep a passed in parameter.
        run_your_program $@          #use all your passed parameters.
        echo -e '\n-------------'    #use your single quotes.
        echo `date`                  #use your back ticks.
        echo -e "\n-------------"    #use your double quotes.
    }
    alias foobarbaz=lets_do_some_stuff
    

    Then you can use your $1 and $2 variables and single, double quotes and back ticks without worrying about the alias function wrecking their integrity.

    This program prints:

    el@defiant ~/code $ foobarbaz alien Dyson ring detected @grid 10385
    alien Dyson ring detected @grid 10385
    -------------
    Mon Oct 26 20:30:14 EDT 2015
    -------------
    

提交回复
热议问题