0

1.) I am using Debian 8.4 on a virtual box and as I ran the command wc sample.txt to sample.txt containing:

Hello

The output to the command was

1 1 6 sample.txt

Is the extra character EOF? If it is then how come when I ran the same command for an empty file the output was..

0 0 0 sample.txt
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Patrick
  • 191
  • 2
  • 8
  • Also, cross-site duplicates: http://serverfault.com/questions/287370/why-wc-c-always-count-1-more-character http://superuser.com/questions/525902/linux-shell-wc-c-count-characters-1 http://unix.stackexchange.com/questions/85268/why-do-i-get-one-more-number-of-bytes-reported-in-a-file – tripleee Apr 22 '16 at 08:19

1 Answers1

4

You have a trailing new line and this is what wc reports.

See for example if we create a file with printf:

$ printf "hello" > a
$ cat a | hexdump -c
0000000   h   e   l   l   o
0000005
$ wc a
0 1 5 a

However, if we write with something like echo, a trailing new line is appended:

$ echo "hello" > a
$ cat a | hexdump -c
0000000   h   e   l   l   o  \n
0000006
$ wc a
1 1 6 a
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • If it was a newline character, wouldnt wc output 2 lines instead of 1? – Patrick Apr 20 '16 at 12:48
  • No, `\n` is the last character in the first line. – fedorqui Apr 20 '16 at 12:51
  • @Patrick and, in fact, `wc` counts how many new lines the file contains. That's why my first example shows `0` in the first column. – fedorqui Apr 20 '16 at 12:56
  • 1
    I ran the code above and it is indeed a newline character. I didnt expect a newline to be added even when I had not inputted one. Thanks! – Patrick Apr 20 '16 at 12:59
  • 1
    @Patrick it is very common to have newlines appended to the end of streams. This also surprised me, so it triggered me ask [Trailing new line after piping to a command: is there any standard?](http://stackoverflow.com/q/36641445/1983854). – fedorqui Apr 20 '16 at 13:00
  • 1
    Interesting question. I will dig into it once I am more familiar with the shell since I am still a beginner and these things tend to become quite overwhelming. – Patrick Apr 20 '16 at 13:15
  • 2
    You can see the exact contents of your file using `od`. e.g. `od -c sample.txt`. – Mort Apr 20 '16 at 15:13