Bash 为什么测试第一个字符会跳过空行?

Bash 为什么测试第一个字符会跳过空行?,bash,Bash,为什么下面的文件读取器和注释行检查会跳过空行 while read line; do if [ ! ${line:0:1} == "#" ]; then # leave out comments function_call $line fi done < list_items.txt 读行时;做 如果[!${line:0:1}==“#”];然后,省去评论 函数调用$line fi 完成

为什么下面的文件读取器和注释行检查会跳过空行

while read line; do
    if [ ! ${line:0:1} == "#" ]; then # leave out comments
            function_call $line
    fi
done < list_items.txt
读行时
;做
如果[!${line:0:1}==“#”];然后,省去评论
函数调用$line
fi
完成
跳过空行的行为是由不带引号的bash变量引起的,在比较字符串时始终带引号:

while read line; do
    if [ ! "${line:0:1}" == "#" ]; then # leave out comments
            function_call "${line}"
    fi
done < list_items.txt
读行时
;做
如果[!“${line:0:1}”==“#”];然后,省去评论
函数_调用“${line}”
fi
完成

上面将处理空行。

更有趣的是,为什么
[!='#']
返回false。函数调用参数${line}上的引号似乎使其成为单个参数,这在我的例子中破坏了函数。