bash: read line and keep spaces

倖福魔咒の 提交于 2019-11-29 11:08:01

Disable word splitting by clearing the value of IFS (the internal field separator):

while IFS= read -r line; do
....
done < "$1"

The -r isn't strictly necessary, but it is good practice.


Also, a simpler way to check the value of line (I assume you're looking for a line with nothing but whitespace):

if [[ $line =~ ^$ ]]; then
    echo "Fake empty line detected"
fi

Following your code, it can be improved.

while read line; do
        if [ -z "$line" ]
        then
             echo "Fake empty line detected"
        fi
done < "$1"

The test -z checks if $line is empty.

Output:

Fake empty line detected
Fake empty line detected
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!