-1

When I run

echo "obase=2;3" | bc | grep -v \n\s | wc -m

bash returns 3. But when I run

echo "obase=2;3" | bc

bash returns 11.

Why is wc -m one digit high on its count?

jeffmjack
  • 572
  • 4
  • 14

1 Answers1

4

The extra character is the trailing newline.

wc -m receives and counts the following three characters: 1 1 \n.

$ echo "obase=2;3" | bc | grep -v \n\s | od -c
0000000    1   1  \n                                                    
0000003

If you get rid of the newline, the count will be as you're expecting:

$ echo "obase=2;3" | bc | grep -v \n\s | tr -d '\n' | wc -m
       2
NPE
  • 486,780
  • 108
  • 951
  • 1,012