This really confused me, when I do a cat cmd, I always see something like % at the end of a file, but if I open the file on editor or sublime, it is gone. So how to make sure my file is in a correct format for example, run cmd cat -e test.txt


It's zsh's way of showing that your file ends with an unterminated line. You'd see something similar with the fish shell which (confusingly) uses a character instead where available or ^J otherwise.

With most other shells (or with zsh if you turn off this feature), you'd see the prompt immediately after afg on the same line, and with some shells there would be display glitches because the shell expects the prompt to be at the left margin.

Normally, on Unix-like systems, the newline character is a line terminator: every line must end with a newline character. A text file consists of a sequence of lines, so a non-empty text file always ends with a newline character. (This is different from Windows where the newline character sequence (CR+LF) is usually a line separator, and so text files often don't end with a line break.)

Some text processing tools do not process the last line of a file correctly if it isn't terminated by a newline, although most modern programs will automatically add a final newline character if one is missing. A missing final newline can have surprising consequences; for example, if you concatenate the file with another file, afg will be concatenated with the first line of the other file, not on a line of its own.

Many editors automatically end files with a newline, so that they're valid text files. The ones that don't systematically do it by default can usually be configured to do so.

Not the answer you're looking for? Browse other questions tagged or ask your own question.