3

I was originally of the impression that

$ ./myprog
moo[CTRL-D]

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
moo[CTRL-D]0

(Apparently zero lines is possible?)

$ echo moo > cow
$ wc -l < cow
1

Can anyone explain this to me?

4

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