将 echo 的输出拆分为两行

将 echo 的输出拆分为两行

手册上写着/n会将 echo 命令的输出拆分到下一行。我试过了:

echo -e 'hello /n world'
hello /n world

期望下一行是“world”。我失败了。

答案1

您的错误在于使用/n而不是\n

所以只要尝试一下echo -e 'hello \nworld'你就会得到你所期望的。

\n请注意,我删除了和之间的空格world。否则第二行将以空格开头。

答案2

\n不是新行/n

你应该尝试这个:

echo -e 'hello \n world'

答案3

man echo

   -e     enable interpretation of backslash escapes

   -E     disable interpretation of backslash escapes (default)

   If -e is in effect, the following sequences are recognized:

   \\     backslash

   \a     alert (BEL)

   \b     backspace

   \c     produce no further output

   \e     escape

   \f     form feed

   \n     new line

   \r     carriage return

   \t     horizontal tab

   \v     vertical tab

   \0NNN  byte with octal value NNN (1 to 3 digits)

   \xHH   byte with hexadecimal value HH (1 to 2 digits)

   NOTE: your shell may have its own version of echo, which usually supersedes the version described here. Please refer to your shell's documentation for details about the options it supports.

请注意注意事项:)例如sh没有-e选项echo

$ sh -c "echo -e 'hello \n world'"
-e hello 
world

您可以看到-e输出为常规文本,但反斜杠转义被按预期进行解释。

答案4

一种直接的替代方法是在引号字符串中使用显式换行符,

echo "hello
world"

如果要查看对齐情况,请在第一个引号字符后按 Enter

echo "
hello beautiful
wonderful world"

并删除该换行符以避免输出的第一行空白。

相关内容