문제

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.

도움이 되었습니까?

해결책

Its also counting the newline, try

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

-n tells the echo not to print a newline.

다른 팁

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

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top