Domanda

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.

È stato utile?

Soluzione

Its also counting the newline, try

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

-n tells the echo not to print a newline.

Altri suggerimenti

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top