سؤال

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