简体   繁体   English

如何在外壳中使echo或printf忽略双反斜杠(\\\\)并按我看到的方式打印

[英]How can I make echo or printf in shell ignore a double backslash (\\) and print it as I see it

I'm trying to enter some ascii word art into my shell script and I'm using two backslashes (\\\\) together I want echo or printf to print both these backslashes together. 我试图在我的shell脚本中输入一些ascii艺术字,并且我同时使用两个反斜杠(\\\\),我希望echo或printf将这两个反斜杠一起打印。 Is there something I can put after echo or printf to make them literally print every character within the double quotes (")? 我可以在echo或printf之后添加一些内容,以使它们在字面上打印双引号(“)中的每个字符吗?

for example I want: 例如我想要:

echo -something "
     __      __  __      __
      \\    //    \\    //
       \\  //      \\  //
        \\//        \\//
"

to print 打印

     __      __  __      __
      \\    //    \\    //
       \\  //      \\  //
        \\//        \\//

Exactly how it is. 确实是这样。

Single quotes are your friend; 单引号是您的朋友; backslash is just an ordinary character inside a single-quoted string. 反斜杠只是单引号中的普通字符。 So is every other character except single-quote itself. 除单引号本身以外的所有其他字符也是如此。

echo '
     __      __  __      __
      \\    //    \\    //
       \\  //      \\  //
        \\//        \\//
'

If you needed single quotes in the single-quoted output, you have to write the sequence '\\'' to get the single quote: 如果在单引号输出中需要单引号,则必须编写序列'\\''来获得单引号:

echo '"single quotes" ('\'') are harder than "backslashes" (\)'

where I included double quotes and backslashes to make it harder to use double quotes around the string. 我在其中添加了双引号和反斜杠,以使在字符串周围使用双引号变得更加困难。 However, you could also write: 但是,您也可以这样写:

echo "\"single quotes\" (') are harder than \"backslashes\" (\\)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM