'wc -m' shows one character too many2019 Community Moderator ElectionWhy does echo “a” | wc -m return 2?How does word count work for newline (-l)? Number of newlines or linesPrint folders names character countCharacter count in Unix wc commandwc - setting a terminating characterHow many lines of code are in this file?`ls / | wc -l` tells more lines than `ls /` showsline(records) Count and grep together in a one command on a dat fileHow to find how many paragraphs in a file?sort Find how many distinct values are among the certain columnwc -c gives me one extra character countsearching letters in one line using grep and wc
It grows, but water kills it
What is going on with 'gets(stdin)' on the site coderbyte?
Quoting Keynes in a lecture
Extract more than nine arguments that occur periodically in a sentence to use in macros in order to typset
Is this toilet slogan correct usage of the English language?
When were female captains banned from Starfleet?
What does "Scientists rise up against statistical significance" mean? (Comment in Nature)
Why is the "ls" command showing permissions of files in a FAT32 partition?
How does a computer interpret real numbers?
Yosemite Fire Rings - What to Expect?
Is aluminum electrical wire used on aircraft?
PTIJ: Haman's bad computer
Fear of getting stuck on one programming language / technology that is not used in my country
How to explain what's wrong with this application of the chain rule?
Redundant comparison & "if" before assignment
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
Hero deduces identity of a killer
How does the math work for Perception checks?
Why should universal income be universal?
What should you do if you miss a job interview (deliberately)?
Unexpected behavior of the procedure `Area` on the object 'Polygon'
How to rewrite equation of hyperbola in standard form
Why can Carol Danvers change her suit colours in the first place?
How much character growth crosses the line into breaking the character
'wc -m' shows one character too many
2019 Community Moderator ElectionWhy does echo “a” | wc -m return 2?How does word count work for newline (-l)? Number of newlines or linesPrint folders names character countCharacter count in Unix wc commandwc - setting a terminating characterHow many lines of code are in this file?`ls / | wc -l` tells more lines than `ls /` showsline(records) Count and grep together in a one command on a dat fileHow to find how many paragraphs in a file?sort Find how many distinct values are among the certain columnwc -c gives me one extra character countsearching letters in one line using grep and wc
I am writing a program on using wc -m
/wc -c
to count the amount of letters in a string. It counts the letters, but adds one extra. This is what I currently have:
echo "enter a word"
read var1
echo $var1 | wc -c
When I enter the word, it successfully does something, however, for example if I enter "test" it will output 5.
Why is it doing this and is there a fix?
wc
add a comment |
I am writing a program on using wc -m
/wc -c
to count the amount of letters in a string. It counts the letters, but adds one extra. This is what I currently have:
echo "enter a word"
read var1
echo $var1 | wc -c
When I enter the word, it successfully does something, however, for example if I enter "test" it will output 5.
Why is it doing this and is there a fix?
wc
Related: How does word count work for newline (-l)? Number of newlines or lines
– slm♦
Oct 20 '14 at 12:35
add a comment |
I am writing a program on using wc -m
/wc -c
to count the amount of letters in a string. It counts the letters, but adds one extra. This is what I currently have:
echo "enter a word"
read var1
echo $var1 | wc -c
When I enter the word, it successfully does something, however, for example if I enter "test" it will output 5.
Why is it doing this and is there a fix?
wc
I am writing a program on using wc -m
/wc -c
to count the amount of letters in a string. It counts the letters, but adds one extra. This is what I currently have:
echo "enter a word"
read var1
echo $var1 | wc -c
When I enter the word, it successfully does something, however, for example if I enter "test" it will output 5.
Why is it doing this and is there a fix?
wc
wc
edited yesterday
ilkkachu
62.4k10103179
62.4k10103179
asked Oct 20 '14 at 12:24
Joe DaleJoe Dale
365
365
Related: How does word count work for newline (-l)? Number of newlines or lines
– slm♦
Oct 20 '14 at 12:35
add a comment |
Related: How does word count work for newline (-l)? Number of newlines or lines
– slm♦
Oct 20 '14 at 12:35
Related: How does word count work for newline (-l)? Number of newlines or lines
– slm♦
Oct 20 '14 at 12:35
Related: How does word count work for newline (-l)? Number of newlines or lines
– slm♦
Oct 20 '14 at 12:35
add a comment |
1 Answer
1
active
oldest
votes
This has to do with the command you're using to pipe the string to wc
. The echo
command is slipping in an extra character at the end of your string, test
, a new newline character, n
.
So in effect you're counting this: testn
. You can disable this behavior with the -n
switch to echo
.
$ echo -n "test" | wc -c
4
Or use a different command to generate your string, such as printf
:
$ printf "%s" "test" | wc -c
4
Seeing what's happening
You can use od
to see the actual characters that are getitng passed to the pipe like so:
$ echo "test" | od -c
0000000 t e s t n
0000005
$ echo -n "test" | od -c
0000000 t e s t
0000004
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f163141%2fwc-m-shows-one-character-too-many%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This has to do with the command you're using to pipe the string to wc
. The echo
command is slipping in an extra character at the end of your string, test
, a new newline character, n
.
So in effect you're counting this: testn
. You can disable this behavior with the -n
switch to echo
.
$ echo -n "test" | wc -c
4
Or use a different command to generate your string, such as printf
:
$ printf "%s" "test" | wc -c
4
Seeing what's happening
You can use od
to see the actual characters that are getitng passed to the pipe like so:
$ echo "test" | od -c
0000000 t e s t n
0000005
$ echo -n "test" | od -c
0000000 t e s t
0000004
add a comment |
This has to do with the command you're using to pipe the string to wc
. The echo
command is slipping in an extra character at the end of your string, test
, a new newline character, n
.
So in effect you're counting this: testn
. You can disable this behavior with the -n
switch to echo
.
$ echo -n "test" | wc -c
4
Or use a different command to generate your string, such as printf
:
$ printf "%s" "test" | wc -c
4
Seeing what's happening
You can use od
to see the actual characters that are getitng passed to the pipe like so:
$ echo "test" | od -c
0000000 t e s t n
0000005
$ echo -n "test" | od -c
0000000 t e s t
0000004
add a comment |
This has to do with the command you're using to pipe the string to wc
. The echo
command is slipping in an extra character at the end of your string, test
, a new newline character, n
.
So in effect you're counting this: testn
. You can disable this behavior with the -n
switch to echo
.
$ echo -n "test" | wc -c
4
Or use a different command to generate your string, such as printf
:
$ printf "%s" "test" | wc -c
4
Seeing what's happening
You can use od
to see the actual characters that are getitng passed to the pipe like so:
$ echo "test" | od -c
0000000 t e s t n
0000005
$ echo -n "test" | od -c
0000000 t e s t
0000004
This has to do with the command you're using to pipe the string to wc
. The echo
command is slipping in an extra character at the end of your string, test
, a new newline character, n
.
So in effect you're counting this: testn
. You can disable this behavior with the -n
switch to echo
.
$ echo -n "test" | wc -c
4
Or use a different command to generate your string, such as printf
:
$ printf "%s" "test" | wc -c
4
Seeing what's happening
You can use od
to see the actual characters that are getitng passed to the pipe like so:
$ echo "test" | od -c
0000000 t e s t n
0000005
$ echo -n "test" | od -c
0000000 t e s t
0000004
answered Oct 20 '14 at 12:32
slm♦slm
254k71538687
254k71538687
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f163141%2fwc-m-shows-one-character-too-many%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Related: How does word count work for newline (-l)? Number of newlines or lines
– slm♦
Oct 20 '14 at 12:35