为什么回声拆分长行80个字符引号内打印时? (以及如何解决?) [英] Why echo splits long lines in 80 chars when printing within quotes? (And how to fix it?)

查看:188
本文介绍了为什么回声拆分长行80个字符引号内打印时? (以及如何解决?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回响不带引号... 1行。精细。

Echoing without quotes... 1 line. Fine.

$ echo $(ls -1dmb /bin/*) > test
$ wc -l test
1 test

与报价... 396线相呼应。坏的。

Echoing with quotes... 396 lines. Bad.

$ echo "$(ls -1dmb /bin/*)" > test
$ wc -l test
396 test

问题使用echo写一个文件,并展开一个长变量时,来了。

The problem comes when using echo for writing a file and expanding a long variable.

为什么会出现这种情况?如何解决呢?

Why does this happen? How to fix it?

推荐答案

LS 正在检测你的标准输出是不是终端

ls is detecting that your stdout is not a terminal.

检查 LS输出-1dmb /斌/ * |猫 VS LS -1dmb /斌/ * 。这是 LS ,谁是分裂的输出。

check the output of ls -1dmb /bin/* | cat vs ls -1dmb /bin/*. It's ls, who is splitting the output.

同样,对于 LS --color =汽车情况下,颜色选项时,根据是否标准输出是终端还是不行。

Similarly, for ls --color=auto case, color option is used, based on whether the stdout is terminal or not.

在报价的使用,回声装有一个参数,已经嵌入换行,空格,这是呼应的,是到文件。

When quotes are used, echo is provided with a single arguments, which has embedded newlines, spaces, which are echoed as-is to file.

在不使用引号,回声提供了多个参数,这是由的 IFS 。因此回声打印所有的人都在一行。
但是,不要跳过这些报价 ...

When quotes are not used, echo is provided multiple arguments, which are split by the IFS. Thus echo prints all of them in a single line. But, don't skip these quotes...

如何解决它:

我认为,分裂总是出现在一些文件名和放大器的结束;从来没有在文件名之间。所以这2个选项之一,可为你工作:

I think, the splitting always occurs at the end of some file name & never in between a filename. So one of these 2 options may work for you:

ls -1dmb /bin/* | tr '\n' ' ' >test
ls -1dmb /bin/* | tr -d '\n' >test

这篇关于为什么回声拆分长行80个字符引号内打印时? (以及如何解决?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆