如何打印新行?这只会打印\n:
$ echo -e "Hello,\nWorld!"
Hello,\nWorld!
如何打印新行?这只会打印\n:
$ echo -e "Hello,\nWorld!"
Hello,\nWorld!
当前回答
这最好作为
x="\n"
echo -ne $x
-e选项将解释转义序列的反斜杠-n选项将删除输出中的尾随换行符
PS:命令echo的效果是始终在输出中包含一个尾随换行符,因此需要-n来关闭该功能(并使其不那么混乱)
其他回答
改用printf:
printf "hello\nworld\n"
printf在不同环境中的表现比echo更一致。
你也可以使用带括号的echo,
$ (echo hello; echo world)
hello
world
要打印带有回音的新行,请使用:
echo
or
echo -e '\n'
str='hello\nworld'
$ echo | sed "i$str"
hello
world
如果您正在编写脚本,并且将多次将换行符作为其他消息的一部分进行回显,那么一个不错的跨平台解决方案是在变量中添加一个文本换行符,如下所示:
newline='
'
echo "first line${newline}second line"
echo "Error: example error message n${newline}${usage}" >&2 #requires usage to be defined