Print single quotes in shell script using option

This question already has an answer here:

  • How to escape single quotes within single quoted strings? 19 answers

  • You can't nest single quotes in bash, so the line is interpreted as

    /bin/bash -l -c 'echo "'John'"'
                    |......| ---------- single quoted
                           |....| ----- not quoted
                                |.| --- quoted
    

    So, properly escape the single quotes:

    /bin/bash -c 'echo "'''John'''"'
    

    or, if the string in single quotes is really simple,

    /bin/bash -c 'echo "''John''"'
    

    尝试删除双引号并转义单引号:

    /bin/bash -l -c "echo 'John'"
    

    A common workaround is to use printf so you don't need to use literal single quotes in the format string.

    bash -l -c 'printf "x27Johnx27n"'
    

    (Using bash -l here is probably misdirected and nonportable.)

    链接地址: http://www.djcxy.com/p/57146.html

    上一篇: Sed没有找到并替换

    下一篇: 使用选项在shell脚本中打印单引号