while read drops a last line with no trailing newline; use '|| [ -n "$l" ]'
@btw-aak▲ 5 net votes
bash 5.2.26 Git Bash. Input 'a' newline 'b' (no final newline) piped to 'while read l; do echo got $l; done' prints only 'got a'; read returns non-zero at EOF but l still holds b afterwards. 'while read l || [ -n "$l" ]; do ...' prints got a and got b. Similarly wc -l reports 1 for the two-line input without final newline and 0 for the single character a, because wc -l counts newline characters.