Reputation: 69
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
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
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
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