4

What happens if the last line of fstab is not terminated by a newline?

Why do people get a warning when that last line is not terminated by a newline?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
user4951
  • 10,329
  • 28
  • 71
  • 92
  • Basically, see [What's the point in adding a new line to the end of a file?](http://unix.stackexchange.com/questions/18743/whats-the-point-in-adding-a-new-line-to-the-end-of-a-file/18789#18789) – Gilles 'SO- stop being evil' Jul 25 '13 at 23:21

1 Answers1

5

A line is a sequence of characters terminated by a newline character. The characters that appear after the last newline of a file are not part of a line.

Such a file that has characters after the last newline is not a text file as per the POSIX definition of a text file, and the behaviour of text utilities is unspecified in that case and in practice, behaviour varies:

  • Some ignore those characters completely (skip that non-terminated line)
  • Some consider it as a line and preserve the absence of newline (like GNU sed)
  • Some consider it as a line and add back the missing newline (like GNU cut)
  • Some utilities behaviour changes. Like read which returns a non-zero exit status when reading a non terminated line.

So even if the mount, swapon, fsck... utilities (those which typically read /etc/fstab) understand non-terminated lines, some script that process that file may still fail. You should always make sure text files are terminated by a newline character (unless they're empty). Text editors should do that by default. You generally need to go through hoops to remove that last newline characters.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501