Why does "wc -c" appear to add an extra byte?

C

CrazedNerd

Guest
Code:
sisyphus@sisyphus:~/c$ echo hello | wc -c
6

I'm pretty sure the word "hello" has five characters, why does wc say that there are 6 bytes in hello?
 


Code:
sisyphus@sisyphus:~/c$ echo hello | wc -c
6

I'm pretty sure the word "hello" has five characters, why does wc say that there are 6 bytes in hello?
The echo command adds a trailing newline character. The trailing newline character is counted by the wc command in your example. For the echo command, you can omit the trailing newline character with the "-n" switch.

Code:
$ echo hello
hello
$ echo -n hello
hello$ # Observe: No newline before the prompt.
$ echo hello | wc -c
6
$ echo -n hello | wc -c
5
$

Hints:
  • All of these are different ways to represent the newline character in Unix, Linux, and current Macs:
    • \n
    • "line feed"
    • CTRL-J
    • ASCII 10, Hex 0x0A
    • "LF"
  • In Unix and Linux and any Mac made in the last 20 years, the newline character is a single character.
  • -> In Windows, the newline is a sequence of two characters: "carriage return" and then "line feed".
  • On very old Macs that run the ancient "classic" MacOS operating system, newline was a single "carriage return" character.
 
The echo command adds a trailing newline character. The trailing newline character is counted by the wc command in your example. For the echo command, you can omit the trailing newline character with the "-n" switch.

Code:
$ echo hello
hello
$ echo -n hello
hello$ # Observe: No newline before the prompt.
$ echo hello | wc -c
6
$ echo -n hello | wc -c
5
$

Hints:
  • All of these are different ways to represent the newline character in Unix, Linux, and current Macs:
    • \n
    • "line feed"
    • CTRL-J
    • ASCII 10, Hex 0x0A
    • "LF"
  • In Unix and Linux and any Mac made in the last 20 years, the newline character is a single character.
  • -> In Windows, the newline is a sequence of two characters: "carriage return" and then "line feed".
  • On very old Macs that run the ancient "classic" MacOS operating system, newline was a single "carriage return" character.
That is correct, because when you turn off the newline feature:

Code:
sisyphus@sisyphus:~/c$ echo -n hello | wc -c
5


The strings are probably null-terminated and the null character takes a byte.
Is the null character the same thing as a newline?
 
Last edited by a moderator:

Members online


Top