本文介绍了庆典读行不读前导空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件 A.TXT 与以下内容

  AAA
    BBB

当我执行下面的脚本,

 而读线

    回声$线
完成< A.TXT> b.txt

生成的 b.txt 包含以下

  AAA
BBB

可以看出,线前导空格已经得到删除。

我如何preserve前导空格?


解决方案

这是覆盖在。

As Charles Duffy correctly points out (and I'd missed by focusing on the IFS issue); if you want to see the spaces in your output you also need to quote the variable when you use it or the shell will, once again, drop the whitespace.

Notes about some of the other differences in that quoted snippet as compared to your original code.

The use of the -r argument to read is covered in a single sentence at the top of the previously linked page.

As to using printf instead of echo there the behavior of echo is, somewhat unfortunately, not portably consistent across all environments and the differences can be awkward to deal with. printf on the other hand is consistent and can be used entirely robustly.

这篇关于庆典读行不读前导空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:15
查看更多