Kenny Wang
Kenny Wang

Reputation: 69

wc -c gives one more than I expected, why is that?

echo '2003'| wc -c

I thought it would give me 4, but it turned to be 5, what is that additional byte?

Upvotes: 1

Views: 2672

Answers (3)

Neil
Neil

Reputation: 11

echo contains a built-in switch, -n, to remove newline. So running:

echo -n "2021" | wc -c

Will output the expected 4.

Upvotes: 1

Narasimharao
Narasimharao

Reputation: 1

echo adds new line which is causing the issue. As mentioned by "KyChen", you can use printf or:

a="2014 ;
echo $a |awk '{print length}'

Upvotes: 0

KyChen
KyChen

Reputation: 59

Because echo will get a new line.

echo "2014" | wc -c

it will get 5

printf "2014" | wc -c

it will get 4 where printf will not add a new line.

Upvotes: 3

Related Questions