如何让cat在每行后添加文字换行符? (对于echo -e)在bash中[重复]

时间:2022-03-01 03:40:59

This question already has an answer here:

这个问题在这里已有答案:

How can I get cat to add a literal line break after each line? (for echo -e to read it) in bash

如何让cat在每行后添加文字换行符? (用于回显-e来读取它)在bash中

root@111[~]# cat names 
Joe Smith
John St. John
Jeff Jefferson
root@111 [~]# var=`cat names`;echo -e $var
Joe Smith John St. John Jeff Jefferson

I want the second command to produce output identical to the first.

我希望第二个命令产生与第一个命令相同的输出。

1 个解决方案

#1


First, don't use echo -e; it adds no value here, and the POSIX standard for echo explicitly disallows the behavior you're expecting it to provide (a POSIX-compliant echo with XSI extensions will honor backslash escapes without -e; a POSIX-compliant echo without XSI extensions will just echo the literal string -e and still ignore backslash escapes; the GNU implementation that changes its behavior based on whether -e breaks both XSI and baseline versions of the standard).

首先,不要使用echo -e;它在这里没有增加任何价值,并且echo的POSIX标准明确禁止你期望它提供的行为(符合POSIX的回声与XSI扩展将在没有-e的情况下支持反斜杠转义;没有XSI扩展的符合POSIX的回声将只是回显文字字符串-e并仍然忽略反斜杠转义; GNU实现根据-e是否同时破坏标准的XSI和基线版本来改变其行为。

Second, use quotes:

二,使用引号:

echo "$var"

...or, better, skip echo altogether in favor of printf:

...或者,更好的是,完全跳过echo以支持printf:

printf '%s\n' "$var"

#1


First, don't use echo -e; it adds no value here, and the POSIX standard for echo explicitly disallows the behavior you're expecting it to provide (a POSIX-compliant echo with XSI extensions will honor backslash escapes without -e; a POSIX-compliant echo without XSI extensions will just echo the literal string -e and still ignore backslash escapes; the GNU implementation that changes its behavior based on whether -e breaks both XSI and baseline versions of the standard).

首先,不要使用echo -e;它在这里没有增加任何价值,并且echo的POSIX标准明确禁止你期望它提供的行为(符合POSIX的回声与XSI扩展将在没有-e的情况下支持反斜杠转义;没有XSI扩展的符合POSIX的回声将只是回显文字字符串-e并仍然忽略反斜杠转义; GNU实现根据-e是否同时破坏标准的XSI和基线版本来改变其行为。

Second, use quotes:

二,使用引号:

echo "$var"

...or, better, skip echo altogether in favor of printf:

...或者,更好的是,完全跳过echo以支持printf:

printf '%s\n' "$var"