How does the wc -c in linux work?
16,803
Solution 1
Its also counting the newline, try
[~]> echo -n abc|wc -c
3
-n
tells the echo
not to print a newline.
Solution 2
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
Solution 3
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
Author by
Pankaj Mahato
Updated on June 13, 2022Comments
-
Pankaj Mahato almost 2 years
Why the number of characters is
4
?echo abc|wc -c
Output
4
The output should be
3
, because the number of characters is3
.