Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why wc adds plus one

Tags:

bash

wc

Lets say I have a word of length N.

word0=`echo` # N = 0  
word1=`echo A` # N = 1
word2=`echo AB` # N = 2
word5=`echo ABCDE` # N = 5
word4=`echo "ABCD"` # N = 4

I use wc to get the length ofwordN, for example:

echo $word0 | wc 
   1 
echo $word1 | wc 
   2 
echo $word4 | wc 
   5

wc adds +1 to the word length and result is N+1

Even with wc -c or wc -m I got N+1

Question: Should wc work like this? If so, why it adds +1?

like image 473
pogibas Avatar asked Oct 15 '25 14:10

pogibas


1 Answers

try:

echo -n "stuff"|wc

echo adds a newline, so if you count by bytes or chars, there is at least 1

see following examples:

kent$  echo ""|wc -c
1

kent$  echo -n ""|wc -c
0

kent$  echo  ""|wc -m
1

kent$  echo  -n ""|wc -m
0

if you count by "word", there is no difference:

kent$  echo  -n ""|wc -w
0

kent$  echo  ""|wc -w   
0
like image 172
Kent Avatar answered Oct 18 '25 10:10

Kent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!