echo 和 cat - 抑制新行字符

发布于 2024-12-21 15:25:02 字数 298 浏览 6 评论 0原文

下面是示例。我可以看到,当我将 echo 与 cat 一起使用时,它会抑制新行字符。这是预期的行为吗?我正在Solaris unix 上工作。

   .262> cat temp2.txt 
    20110801
    20110802
    20110803
    20110804
    20110805
    20110808
    > echo `cat temp2.txt `
    20110801 20110802 20110803 20110804 20110805 20110808

Below is the example.I can see that when i use echo with cat it suppresses the new line characters.Is this the expected behaviour?I am working on solaris unix.

   .262> cat temp2.txt 
    20110801
    20110802
    20110803
    20110804
    20110805
    20110808
    > echo `cat temp2.txt `
    20110801 20110802 20110803 20110804 20110805 20110808

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤者何惧 2024-12-28 15:25:02

是的,这是预期的行为。

但正如“Mark Reed”建议的那样,您可以通过使用引号来保留空格:

echo "$(cat temp2.txt)"

Yes this is the expected behaviour.

But as "Mark Reed" suggested you can keep the white space by using quotes:

echo "$(cat temp2.txt)"
娇俏 2024-12-28 15:25:02

是的,这是预期的行为。

我不知道有哪个 shell 不这样做。

它记录在 bash 中:

 $ man bash | awk '/Command Substitution/ { p = 1 } /Arithmetic Expansion/ { p = 0 } { if (p) print; }'

在 zsh 中:(

$ man zshall | awk '/COMMAND SUBSTITUTION/ {p=1} /ARITHMETIC EXPANSION/ {p=0} { if (p) print }'

编辑:我删除了命令行的输出,因为它们毫无意义,正如 @sehe 在评论中指出的那样)

Yes this is expected behavior.

I don't know of any shell that doesn't do it.

It is documented in bash:

 $ man bash | awk '/Command Substitution/ { p = 1 } /Arithmetic Expansion/ { p = 0 } { if (p) print; }'

In zsh:

$ man zshall | awk '/COMMAND SUBSTITUTION/ {p=1} /ARITHMETIC EXPANSION/ {p=0} { if (p) print }'

(Edit: I removed the output of the command lines as they are pointless, as pointed out by @sehe in the comment)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文