Why there is a difference between the output of 'echo $VAR | wc -c' and 'echo $#VAR'?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I'm working on a Bash script and the length of the string contained in a certain variable is one of my conditions. The current string is W5u7TBTzF17GGFV8DBJHvgAAAAI. Initially I've count the string length by the help of wc -c:



$ echo 'W5u7TBTzF17GGFV8DBJHvgAAAAI' | wc -c
28

$ VAR='W5u7TBTzF17GGFV8DBJHvgAAAAI'
$ echo "$VAR" | wc -c
28


But my script condition [[ $#VAR -eq 28 ]] never pass. Then I decided to count the characters on by one. And actually the string length is 27 characters:



W5u7TBTzF17GGFV8DBJHvgAAAAI
---------------------------
123456789012345678901234567
1 2


Also the value of $#VAR is 27:



$ echo "$#VAR"
27


So I'm in wondering from where this difference comes from?










share|improve this question

















  • 1




    This echo -n $VAR | wc -c give 27, so newline character is there
    – George Udosen
    54 mins ago







  • 1




    @GeorgeUdosen, probably this should be the answer :)
    – pa4080
    49 mins ago














up vote
1
down vote

favorite












I'm working on a Bash script and the length of the string contained in a certain variable is one of my conditions. The current string is W5u7TBTzF17GGFV8DBJHvgAAAAI. Initially I've count the string length by the help of wc -c:



$ echo 'W5u7TBTzF17GGFV8DBJHvgAAAAI' | wc -c
28

$ VAR='W5u7TBTzF17GGFV8DBJHvgAAAAI'
$ echo "$VAR" | wc -c
28


But my script condition [[ $#VAR -eq 28 ]] never pass. Then I decided to count the characters on by one. And actually the string length is 27 characters:



W5u7TBTzF17GGFV8DBJHvgAAAAI
---------------------------
123456789012345678901234567
1 2


Also the value of $#VAR is 27:



$ echo "$#VAR"
27


So I'm in wondering from where this difference comes from?










share|improve this question

















  • 1




    This echo -n $VAR | wc -c give 27, so newline character is there
    – George Udosen
    54 mins ago







  • 1




    @GeorgeUdosen, probably this should be the answer :)
    – pa4080
    49 mins ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm working on a Bash script and the length of the string contained in a certain variable is one of my conditions. The current string is W5u7TBTzF17GGFV8DBJHvgAAAAI. Initially I've count the string length by the help of wc -c:



$ echo 'W5u7TBTzF17GGFV8DBJHvgAAAAI' | wc -c
28

$ VAR='W5u7TBTzF17GGFV8DBJHvgAAAAI'
$ echo "$VAR" | wc -c
28


But my script condition [[ $#VAR -eq 28 ]] never pass. Then I decided to count the characters on by one. And actually the string length is 27 characters:



W5u7TBTzF17GGFV8DBJHvgAAAAI
---------------------------
123456789012345678901234567
1 2


Also the value of $#VAR is 27:



$ echo "$#VAR"
27


So I'm in wondering from where this difference comes from?










share|improve this question













I'm working on a Bash script and the length of the string contained in a certain variable is one of my conditions. The current string is W5u7TBTzF17GGFV8DBJHvgAAAAI. Initially I've count the string length by the help of wc -c:



$ echo 'W5u7TBTzF17GGFV8DBJHvgAAAAI' | wc -c
28

$ VAR='W5u7TBTzF17GGFV8DBJHvgAAAAI'
$ echo "$VAR" | wc -c
28


But my script condition [[ $#VAR -eq 28 ]] never pass. Then I decided to count the characters on by one. And actually the string length is 27 characters:



W5u7TBTzF17GGFV8DBJHvgAAAAI
---------------------------
123456789012345678901234567
1 2


Also the value of $#VAR is 27:



$ echo "$#VAR"
27


So I'm in wondering from where this difference comes from?







command-line bash scripts echo wc






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 1 hour ago









pa4080

12.2k52256




12.2k52256







  • 1




    This echo -n $VAR | wc -c give 27, so newline character is there
    – George Udosen
    54 mins ago







  • 1




    @GeorgeUdosen, probably this should be the answer :)
    – pa4080
    49 mins ago












  • 1




    This echo -n $VAR | wc -c give 27, so newline character is there
    – George Udosen
    54 mins ago







  • 1




    @GeorgeUdosen, probably this should be the answer :)
    – pa4080
    49 mins ago







1




1




This echo -n $VAR | wc -c give 27, so newline character is there
– George Udosen
54 mins ago





This echo -n $VAR | wc -c give 27, so newline character is there
– George Udosen
54 mins ago





1




1




@GeorgeUdosen, probably this should be the answer :)
– pa4080
49 mins ago




@GeorgeUdosen, probably this should be the answer :)
– pa4080
49 mins ago










1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










It's the way echo works. Now do



echo koko


You get



georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo koko
koko


But do echo -n koko and you get



georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo -n koko
kokogeorgek@georgek-HP-Pavilion-17-Notebook-PC:~$


So wc is capturing the newline character too. Use



echo -n $VAR | wc -c


To get the desired result. The echo command will add the newline character, so that gets counted too. To remove this and get the real count use the -n option.






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1075384%2fwhy-there-is-a-difference-between-the-output-of-echo-var-wc-c-and-echo%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote



    accepted










    It's the way echo works. Now do



    echo koko


    You get



    georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo koko
    koko


    But do echo -n koko and you get



    georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo -n koko
    kokogeorgek@georgek-HP-Pavilion-17-Notebook-PC:~$


    So wc is capturing the newline character too. Use



    echo -n $VAR | wc -c


    To get the desired result. The echo command will add the newline character, so that gets counted too. To remove this and get the real count use the -n option.






    share|improve this answer
























      up vote
      5
      down vote



      accepted










      It's the way echo works. Now do



      echo koko


      You get



      georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo koko
      koko


      But do echo -n koko and you get



      georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo -n koko
      kokogeorgek@georgek-HP-Pavilion-17-Notebook-PC:~$


      So wc is capturing the newline character too. Use



      echo -n $VAR | wc -c


      To get the desired result. The echo command will add the newline character, so that gets counted too. To remove this and get the real count use the -n option.






      share|improve this answer






















        up vote
        5
        down vote



        accepted







        up vote
        5
        down vote



        accepted






        It's the way echo works. Now do



        echo koko


        You get



        georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo koko
        koko


        But do echo -n koko and you get



        georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo -n koko
        kokogeorgek@georgek-HP-Pavilion-17-Notebook-PC:~$


        So wc is capturing the newline character too. Use



        echo -n $VAR | wc -c


        To get the desired result. The echo command will add the newline character, so that gets counted too. To remove this and get the real count use the -n option.






        share|improve this answer












        It's the way echo works. Now do



        echo koko


        You get



        georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo koko
        koko


        But do echo -n koko and you get



        georgek@georgek-HP-Pavilion-17-Notebook-PC:~$ echo -n koko
        kokogeorgek@georgek-HP-Pavilion-17-Notebook-PC:~$


        So wc is capturing the newline character too. Use



        echo -n $VAR | wc -c


        To get the desired result. The echo command will add the newline character, so that gets counted too. To remove this and get the real count use the -n option.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 48 mins ago









        George Udosen

        17.1k93660




        17.1k93660



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1075384%2fwhy-there-is-a-difference-between-the-output-of-echo-var-wc-c-and-echo%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery