Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

When running

echo abcd | wc -c

it returns 5.
But the word abcd is only 4 characters long.

Is echo printing some special character after the word abcd?

And can I prevent echo from printing that?

share|improve this question
Why is printf better than echo? – mikeserv Aug 21 '14 at 17:55
Please be aware that wc -c does not tell you how many characters something is; it only tells how many bytes it is. – tchrist Aug 22 '14 at 1:58
up vote 20 down vote accepted

echo print newline (\n) at end of line

echo abcd | xxd
0000000: 6162 6364 0a          abcd.

With some echo implementations, you can use -n :

-n do not output the trailing newline

and test:

echo -n abcd | wc -c

With some others, you need the \c escape sequence:

\c: Suppress the <newline> that otherwise follows the final argument in the output. All characters following the '\c' in the arguments shall be ignored.

echo -e 'abcd\c' | wc -c

Portably, use printf:

printf %s abcd | wc -c

(note that wc -c counts bytes, not characters (though in the case of abcd they are generally equivalent). Use wc -m to count characters).

share|improve this answer
Note that the point is that with some echo implementations (all Unix compliant ones at least), it's echo 'abcd\c' that outputs abcd alone. echo -e 'abcd\c' in Unix-compliant echo implementations would output -e abcd. – Stéphane Chazelas Jun 12 at 21:14

If you run echo without the -n option, it writes a newline character after the arguments.  Otherwise, if you typed echo foo, your next shell prompt would appear to the right of the foo.  So wc is counting the newline.

How can I prevent echo from printing that?

echo -n abcd | wc -c
share|improve this answer

By default, echo will print a newline character (\n) after the string (which is why you see the shell prompt on the next line instead of the same line where abcd is printed.)

You can try adding a -n parameter to echo to disable the trailing newline character.

echo -n abcd | wc -c
share|improve this answer

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.