wc -l counts newlines, not lines
A file with text in it where wc -l answers 0. The tool is behaving exactly as specified — it counts newline characters, and the last line of your data may not have one. Nothing reports an error, two standard tools disagree in writing about the same file, and the most common way to loop over a file silently skips its last record.
The symptom
$ printf 'alpha' > p_printf.txt
$ wc -c < p_printf.txt
5
$ wc -l < p_printf.txt
0
Five bytes of text, and the line count is zero. Exit status 0, no warning. The same file, asked by a different tool:
$ grep -c '' p_printf.txt
1
One of these is counting lines. The other is counting newline characters, and there are none, because printf was not given a \n.
Five tools, one file, two different answers
A three-record file with no final newline, against the same file with one. The right-hand column is the control: when the data is properly terminated, everything agrees, which is how you know the left column is measuring a real disagreement and not five broken commands.
$ printf 'alpha\nbeta\ngamma' > noeol.txt
$ printf 'alpha\nbeta\ngamma\n' > eol.txt
| command | noeol.txt (16 bytes) | eol.txt (17 bytes) |
|---|---|---|
wc -l | 2 | 3 |
grep -c '' | 3 | 3 |
awk 'END{print NR}' | 3 | 3 |
sed -n '$=' | 3 | 3 |
while read -r l | 2 | 3 |
Two of the five are short by one. All five exit 0. One byte of difference in the input, and no diagnostic anywhere.
The disagreement is not a bug in any of them. wc -l is specified to count newlines; grep -c '' is specified to count lines, where a trailing fragment counts as one. Both answers are correct for the question each tool was actually asked. The wrong thing in the room is the assumption that those are the same question.
The damage is in the loop, not the count
Nobody is really harmed by a count being off by one. What costs you is that while read — the ordinary way to process a file record by record — is one of the two that drops the last record:
$ while read -r l; do echo " processing $l"; done < noeol.txt
processing alpha
processing beta
gamma was never processed. The loop ran, exited 0, and reported nothing. If that file is a list of things to upload, delete, retry or verify, the last one silently did not happen.
read returns false when it hits end-of-file without a delimiter — but it *has already assigned* the partial line to the variable. So the data is in hand and the loop condition throws it away. The guard is to check for it:
$ while read -r l || [ -n "$l" ]; do echo " processing $l"; done < noeol.txt
processing alpha
processing beta
processing gamma
And the control, because a fix that over-processes is also a bug — the same two loops on the terminated file:
plain : alpha beta gamma
guarded: alpha beta gamma
Identical. The guard adds the missing record and does not invent an extra empty one.
Two standard tools disagree in writing
This is the clearest statement of the contradiction. git stages the same file and prints its own line count in the hunk header:
$ git diff --cached
diff --git a/noeol.txt b/noeol.txt
new file mode 100644
index 0000000..b9e9ab4
--- /dev/null
+++ b/noeol.txt
@@ -0,0 +1,3 @@
+alpha
+beta
+gamma
\ No newline at end of file
+1,3 — three lines. wc -l said two. Same file, same moment.
Note what git does that almost nothing else does: it *says so*, in that last line. diff(1) does too:
$ diff noeol.txt eol.txt
3c3
< gamma
\ No newline at end of file
---
> gamma
Two files whose text is identical, reported as differing, with the reason spelled out. The diff tools are the exception; they have to represent the byte exactly, so they cannot paper over it. Everything else quietly picks one interpretation.
Where my guess was wrong
The hypothesis I started with was that this is widespread in real file trees, and that a scan would turn up plenty of ordinary text files missing their final newline. The measurement says no, and twice.
A first pass over 400 files in a system tree found 153 without a final newline, which looked like it confirmed the idea. It did not: the filter had not excluded binary files, and a line count over a .png or a compiled message catalogue is meaningless anyway. Restricted to text:
=== text files only, neutral system tree /usr/share ===
text files checked: 202 lacking a final newline: 0
=== Python 3.14 stdlib, .py files ===
checked=398 no final newline=0
Zero out of 202. Zero out of 398. Maintained source trees terminate their files, and the 153 was an artefact of my own sloppy filter — a reminder that the first measurement agreeing with you is the one to re-run.
So the honest version of the claim is narrower than the one I set out to make. This does not bite you on checked-in source. It bites you on generated data: a file written by printf without a trailing \n, an export from a tool that omits it, an editor with "insert final newline" turned off, a payload assembled by string concatenation. Data you produced, in other words, rather than data you cloned.
The plumbing decides, invisibly
Worth knowing because it explains contradictory results on what you think is the same data. The same bytes, counted three ways:
$ printf 'a\nb' > v.txt
$ cat v.txt | wc -l
1
$ echo "$(cat v.txt)" | wc -l
2
$ wc -l <<< "$(cat v.txt)"
2
Routing the data through $(...) changes the answer. Command substitution strips trailing newlines, and then echo and <<< each append one — so the quoted, careful-looking version is the one that *adds* a newline that was not in the file. Neither 1 nor 2 is a bug; the pipeline is simply not the same pipeline.
Detect it, then decide
One command, no ambiguity
$(...) strips the trailing newline, so if anything is left the file does not end in one:
$ for f in noeol.txt eol.txt; do printf "%-10s " "$f"; \
if [ -n "$(tail -c1 "$f")" ]; then echo "NO final newline"; \
else echo "ends with a newline"; fi; done
noeol.txt NO final newline
eol.txt ends with a newline
Count with something that counts lines
$ grep -c '' noeol.txt
3
$ awk 'END{print NR}' noeol.txt
3
Both correct on both files, per the table above. If what you want is "how many records are in here", these answer that question; wc -l answers a different one.
Guard the loop rather than trusting the input
while read -r line || [ -n "$line" ]; do
...
done < "$file"
Verified in both directions above. Use it on any file you did not generate yourself, which is most of them.
Fix the producer where you can
If you are writing the file, end it with a newline. This is not a style preference — it is what makes every tool downstream agree with every other one. The -n-less printf, the echo -n, the last write() without "\n": that is where the ambiguity is introduced, and it is cheaper to remove it there than to defend against it in every consumer.
Why this one is worth a page
- It presents as a counting disagreement and lands as missing work. The
off-by-one in wc -l is harmless; the same off-by-one in while read means the last item was never processed.
- Nothing is broken, so nothing reports an error. Every tool here is
behaving to spec, and specs differ.
- The obvious check passes.
catthe file and you see three lines;
open it in an editor and you see three lines. The missing byte is invisible in exactly the views you would use to confirm the file is fine.
- The data's shape depends on plumbing you did not think about. The same
bytes count differently depending on whether they passed through a variable.
The general form: when two tools disagree about the same file, neither is necessarily wrong — find out which question each one is answering. Here one counts delimiters and the other counts records, and the file is the unusual case where those differ.
Measured on
Windows 11, Git Bash MSYSTEM=MINGW64, GNU coreutils wc, GNU grep 3.0, GNU awk, GNU sed and GNU diff as shipped with Git for Windows, bash as the read implementation, git 2.x from the same distribution, Python 3.14 for the stdlib scan, 2026-11-27. The fixture files are three built for this page and named in the text. The two prevalence scans are over /usr/share and the Python 3.14 standard library on this machine; both are installed trees, not a sample of anything wider, and 0 for them is not a claim about code in general. Counts in the table are the measured outputs, each run against both the unterminated and the terminated fixture.