Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

in tmp I type a single character,but wc -c shows 2,why?

share|improve this question
up vote 7 down vote accepted

Because newlines are characters too. Tell your text editor to not add one at the end of the file. No, I don't know how.

share|improve this answer
as a workaround, you could count newlines with wc -l and substract them from the count of wc -c. If you are counting directly from a piped / redirected output, consider using tee for splitting it. – the-wabbit Jul 6 '11 at 6:09

One way is to tr to delete newlines, then you can count the characters.

Standard behavior:

echo HELLO | wc -m
# result: 6
echo -n HELLO | wc -m
# result: 5

To show the count of newline characters found:

echo HELLO | wc -l
# result: 1
echo -n HELLO | wc -l
# result: 0

Strip the newline character and count characters:

echo HELLO | tr -d '\n' | wc -m
# result: 5

Strip the newline character (and possible returns with \r) and count characters for an input file:

tr -d '\n\r' < input.txt | wc -m
share|improve this answer

Sorry for the late answer. But I saw this question just now with no right reply.

Use echo -n Hello | wc -m.

This is faster than deleting the newline.

Sashidhar, Virginia

share|improve this answer
You're assuming that there is any sort of control over the output process. – Ignacio Vazquez-Abrams Dec 12 '13 at 2:30
And this was already discussed in Emeraldo's answer from a year ago. – Michael Hampton Dec 12 '13 at 2:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.