I was originally of the impression that

$ ./myprog

is exactly the same as

$ echo moo > cow
$ ./myprog < cow

But I found that myprog always counts one more \n in the second version than in the first. Why is this?

Turns out wc does the same thing...

$ wc -l

(Apparently zero lines is possible?)

$ echo moo > cow
$ wc -l < cow

Can anyone explain this to me?


echo appends a newline, unless you tell it not to, by putting -n first or \c at the end, or putting -e first and \c at the end or... you really don't want to know all the varieties of echo. Use printf moo > cow and you'll have a file with zero lines.

  • 1
    +1: printf is much more portable than echo. – jw013 Sep 4 '12 at 0:21
  • Technically, the only possible file with zero lines is empty file. A non-empty file without trailing LF is a file without proper termination of the last line. Could you fix misleading wording in the answer? – Incnis Mrsi Aug 19 '15 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy