EOF and unterminated lines
End of file (EOF) means there is no more input to read. A normal newline-terminated record read by read -r succeeds, even when the record is empty. If EOF arrives before the delimiter, read can assign the text it has received and still return a nonzero status. In the example containing last without a newline, all four characters are retained; only the delimiter is missing. Empty input and an empty line are different. Capture $? immediately after read to inspect its status. The examples use fresh files, and printf without \n creates a final record without a newline. Later, the loops chapter will combine status and retained partial text when processing whole files; do not assume every failed read is EOF (timeouts and other errors are possible).
Practical examples
Run each example separately in Bash 5.2 in a fresh empty practice directory, using default shell options and C locale. Output is shown separately; do not enter it as code.
Complete record
printf '%s\n' 'last' > input.txtIFS= read -r line < input.txtstatus=$?printf 'status=%s value=[%s]\n' "$status" "$line"Expected stdout:
status=0 value=[last]Expected stderr:
No output
Overall example exit status: 0
The newline was present, so the read succeeded.
Partial final record
printf '%s' 'last' > input.txtIFS= read -r line < input.txtstatus=$?printf 'status=%s value=[%s]\n' "$status" "$line"Expected stdout:
status=1 value=[last]Expected stderr:
No output
Overall example exit status: 0
The delimiter was absent, but the characters were retained.
Empty line versus empty file
printf '\n' > blank.txt: > empty.txtIFS= read -r a < blank.txts=$?printf 'blank: %s [%s]\n' "$s" "$a"IFS= read -r b < empty.txts=$?printf 'empty: %s [%s]\n' "$s" "$b"Expected stdout:
blank: 0 []empty: 1 []Expected stderr:
No output
Overall example exit status: 0
The colon command does nothing successfully; redirection creates an empty file. Both values are empty but the statuses differ.
Common mistakes
Checking only the text cannot distinguish an empty line from EOF. Checking only status can discard an unterminated final record. Choose an explicit policy for accepting partial data.
Practice
Compare an empty file, a file with one newline and a file containing last without newline. Print the captured status and value for each.
Next: Read arrays and mapfile overview