'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










2















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?










share|improve this question
























  • Related: How does word count work for newline (-l)? Number of newlines or lines

    – slm
    Oct 20 '14 at 12:35
















2















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?










share|improve this question
























  • Related: How does word count work for newline (-l)? Number of newlines or lines

    – slm
    Oct 20 '14 at 12:35














2












2








2








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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











1 Answer
1






active

oldest

votes


















4














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





share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    4














    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





    share|improve this answer



























      4














      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





      share|improve this answer

























        4












        4








        4







        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 20 '14 at 12:32









        slmslm

        254k71538687




        254k71538687



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

            Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

            Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.