C标准指出,文件应以新行结尾,因此,如果EOF位于行尾,则某些命令可能不会忽略该行。举个例子:
$ echo 'one\ntwo\nthree\c' > file.txt $ cat file.txt one two three $ while read line ; do echo "line $line" ; done < file.txt one two
为了确保在上面的示例中该方法正确运行,请添加测试,以便在最后一行不为空的情况下继续执行循环。
$ while read line || [ -n "$line" ] ; do echo "line $line" ; done < file.txt one two three