阅读在bash文件时preserving前导空格 [英] Preserving leading whitespace when reading a file in bash

查看:146
本文介绍了阅读在bash文件时preserving前导空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许有人可以给我我的问题的线索。结果
让我们假设我有这样两行:

Maybe someone could give me a clue for my problem.
Let's suppose I have this two lines:

blablablabla
 blablablabla

(第二行开始用空格)

(the second line begins with a space)

我试图测试在我行的第一个字符:

I tried to test the first character on my line:

while read line
do
    check=${line:0:1}
done < file.txt

在这两种情况下,检查='B'!这很烦人,因为我需要这些信息用于治疗的其余部分。

In both cases, check = 'b' ! That's annoying, because I need this information for the rest of the treatment.

推荐答案

您需要指定 IFS 空字符串,这样不丢弃开头或结尾空白:

You need to specify the empty string for IFS so that read doesn't discard leading or trailing whitespace:

while IFS= read line; do
    check=${line:0:1}
done < file.txt

这篇关于阅读在bash文件时preserving前导空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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