1833056 Members
2581 Online
110049 Solutions
New Discussion

Re: wc can't count

 
SOLVED
Go to solution

wc can't count

Hello Gang. I know there has to be a logical explanation for this. If I:

export foo=5 ; echo $foo | wc

How come wc returns 1 1 2 where 2 is the byte count? I was expecting 1 1 1?

A waste of time for this superior forum, but I was however curious. Thanks in advance ...
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: wc can't count

Hi Weary,

You forget the linefeed (ASCII 0aH) it counts too:

If you had done an echo "${foo}\c" | wc, it would have behaved as expected.

Clay
If it ain't broke, I can fix that.
John Poff
Honored Contributor

Re: wc can't count

Hello Weary,

You get a byte count of 2 because your FOO variable has two bytes in it, the "5" and a new line character.

Try this to see it:

export FOO=5; echo $FOO | od -cx



JP
James R. Ferguson
Acclaimed Contributor

Re: wc can't count

Hi:

Its a newline character. Do this:

# foo=5;echo $foo|wc -c > /tmp/look
# cat -e /tmp/look

The '-e' option of 'cat' prints a "$" at the end of each line prior to a newline, exposing the "problem".

...JRF...

Frank Li
Trusted Contributor

Re: wc can't count

There is a carriage return key :

Have a test:
$echo "5



" | wc

you will get the answer .

Clay's method is very good :

echo "5\c" | wc will display " 0 1 1 " since there is "no line return"(0),
one word and one bye.

Hi Friend