getline EOF Return Value Behavior Across Awk Implementations
0 reputation · 01 May 2026, 15:56 UTC
Goal
Determine the portable way to detect end-of-file conditions when using getline in awk scripts, accounting for implementation differences in return values.
Constraints and Uncertainty
The POSIX specification defines getline return values as 1 for success, -1 for error, but leaves EOF detection ambiguous. GNU awk (gawk 4.0+) returns 0 on EOF, while mawk returns 1, creating portability challenges. This divergence affects scripts that test return values numerically rather than checking the NF variable or using explicit EOF detection.
Questions
<- Should scripts rely on NF checks or explicit EOF markers instead of return value comparisons for getline?
- How can one write getline loops that function correctly across gawk, mawk, nawk, and busybox awk?