You are not logged in.

#1 2008-07-23 19:16:32

daneel971
Member
Registered: 2008-03-28
Posts: 197

wc -m output (no real problem, just a curiosity) [SOLVED]

I'm playing with a bash script - at a given point I need to test the lenght of a variable, and I'm using wc for this. Now

echo 12345678 | wc -m

gives 9. shouldn't be 8?
For every word I use, I noticed the result is always a number greater by one unit than the real characters' number. The same happens if I use "wc -c".
Where's the trick? smile

Last edited by daneel971 (2008-07-23 19:26:53)

Offline

#2 2008-07-23 19:21:00

robertp
Member
From: Warszawa, Poland
Registered: 2007-09-11
Posts: 123

Re: wc -m output (no real problem, just a curiosity) [SOLVED]

Try this:

echo -n 12345678 | wc -m

and this

printf "%d"  12345678 | wc -m

and compare with

printf "%d\n"  12345678 | wc -m

Last edited by robertp (2008-07-23 19:22:50)

Offline

#3 2008-07-23 19:22:57

GGLucas
Member
Registered: 2008-03-13
Posts: 113

Re: wc -m output (no real problem, just a curiosity) [SOLVED]

I think it's echo outputting a newline at the end, and wc counting that.

Offline

#4 2008-07-23 19:26:31

daneel971
Member
Registered: 2008-03-28
Posts: 197

Re: wc -m output (no real problem, just a curiosity) [SOLVED]

Thanks both! You hit the nail...  smile

Offline

#5 2008-07-23 19:43:35

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: wc -m output (no real problem, just a curiosity) [SOLVED]

Also interesting: echo '12345' | hexdump -C

Offline

Board footer

Powered by FluxBB