Bash echo 命令不使用转义字符
bash echo
命令不使用转义字符,例如“\n”和“\t”
echo "This is test string\nAnd this is next line"
对于它显示的上述输入
This is test string\nAnd this is next line
那么如何在下一行打印?
The bash echo
command isn't using the escaped characters like "\n" and "\t"
echo "This is test string\nAnd this is next line"
For the above input it displays
This is test string\nAnd this is next line
So how do I print on the next line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您希望扩展转义字符,则需要
echo -e
:You need
echo -e
if you want escaped characters to be expanded:ANSI-C 引用
ANSI-C Quoting
echo
命令变化很大 - 有些实现会解释其参数中的转义字符,有些则不会,除非您添加-e
选项...有些会打印“ -e” 作为其输出的一部分(如果您尝试将其用作选项)。如果您希望在执行任何重要操作时获得可预测的结果,请改用printf
(请注意,您必须显式包含结尾换行符):当 OS X v10.5 附带一个版本时,我以惨痛的教训学到了这一课带有内置
echo
的 bash 破坏了我的一堆在 v10.4 下运行良好的脚本...The
echo
command varies quite a bit -- some implementations interpret escape characters in their arguments, some don't unless you add the-e
option... some will print "-e" as part of their output if you try to use it as an option. If you want predictable results when doing anything nontrivial, useprintf
instead (note that you must explicitly include the ending newline):I learned this lesson the hard way, when OS X v10.5 came with a version of bash with a builtin
echo
that broke a bunch of my scripts that'd worked just fine under v10.4...您可以使用
echo -e
或在脚本开头使用内置的shopt
:You can use
echo -e
or you can use theshopt
built-in thusly at the beginning of your script: