3

Why the number of characters is 4?

echo abc|wc -c

Output

4

The output should be 3, because the number of characters is 3.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Pankaj Mahato
  • 1,051
  • 5
  • 14
  • 26

3 Answers3

8

Its also counting the newline, try

[~]> echo -n abc|wc -c
3

-n tells the echo not to print a newline.

ismail
  • 46,010
  • 9
  • 86
  • 95
3

echo add a line break at the end of its output, which is counted as a character by wc -c. You can use echo -n to omit the line break, and get the result you're expecting:

[mureinik@mureinik ~]$ echo -n abc | wc -c
3
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

From man wc:

wc - print newline, word, and byte counts for each file

As the rest of the answers indicate, the new line is also counted as character.

See:

$ echo "abc" | wc
      1       1       4
                      ^
                      characters

$ printf "abc" | wc
      0       1       3
                      ^
                      characters
fedorqui
  • 275,237
  • 103
  • 548
  • 598