Search code examples
linuxbashwc

Why does wc count one extra character in my file?


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

Solution

  • 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