Command not working properly in shell script












3















Here is output of:



sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null 


Yash Shah
CoreFragment
CoreFragment_5G
CoreFragment
dlink
Yash Shah
COMFAST
Appbirds_Technologies
SYMBIOSIS
20096641
CoreFragment_5G
AMBER_AP1
REDWING LABS_5G


While the same thing written in a script is not working the same.



Here is a snippet in which I used above command.



for ssid_name in $(sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null)
do
echo "$ssid_name"
done


I got output like this:



Yash
Shah
CoreFragment
CoreFragment_5G
CoreFragment
Yash
Shah
REDWING
LABS
COMFAST
Appbirds_Technologies
SYMBIOSIS
CoreFragment_5G
REDWING
LABS_5G


Note: When there is a space in output it take as a new line.



I'm working on Ubuntu 18.04.










share|improve this question

























  • Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368

    – Haxiel
    Jan 16 at 8:20











  • i didn't find that (might be due to word match) so I created question

    – yash shah
    Jan 16 at 8:23











  • No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.

    – Haxiel
    Jan 16 at 8:25











  • yaa... it's really useful link

    – yash shah
    Jan 16 at 8:40
















3















Here is output of:



sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null 


Yash Shah
CoreFragment
CoreFragment_5G
CoreFragment
dlink
Yash Shah
COMFAST
Appbirds_Technologies
SYMBIOSIS
20096641
CoreFragment_5G
AMBER_AP1
REDWING LABS_5G


While the same thing written in a script is not working the same.



Here is a snippet in which I used above command.



for ssid_name in $(sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null)
do
echo "$ssid_name"
done


I got output like this:



Yash
Shah
CoreFragment
CoreFragment_5G
CoreFragment
Yash
Shah
REDWING
LABS
COMFAST
Appbirds_Technologies
SYMBIOSIS
CoreFragment_5G
REDWING
LABS_5G


Note: When there is a space in output it take as a new line.



I'm working on Ubuntu 18.04.










share|improve this question

























  • Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368

    – Haxiel
    Jan 16 at 8:20











  • i didn't find that (might be due to word match) so I created question

    – yash shah
    Jan 16 at 8:23











  • No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.

    – Haxiel
    Jan 16 at 8:25











  • yaa... it's really useful link

    – yash shah
    Jan 16 at 8:40














3












3








3








Here is output of:



sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null 


Yash Shah
CoreFragment
CoreFragment_5G
CoreFragment
dlink
Yash Shah
COMFAST
Appbirds_Technologies
SYMBIOSIS
20096641
CoreFragment_5G
AMBER_AP1
REDWING LABS_5G


While the same thing written in a script is not working the same.



Here is a snippet in which I used above command.



for ssid_name in $(sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null)
do
echo "$ssid_name"
done


I got output like this:



Yash
Shah
CoreFragment
CoreFragment_5G
CoreFragment
Yash
Shah
REDWING
LABS
COMFAST
Appbirds_Technologies
SYMBIOSIS
CoreFragment_5G
REDWING
LABS_5G


Note: When there is a space in output it take as a new line.



I'm working on Ubuntu 18.04.










share|improve this question
















Here is output of:



sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null 


Yash Shah
CoreFragment
CoreFragment_5G
CoreFragment
dlink
Yash Shah
COMFAST
Appbirds_Technologies
SYMBIOSIS
20096641
CoreFragment_5G
AMBER_AP1
REDWING LABS_5G


While the same thing written in a script is not working the same.



Here is a snippet in which I used above command.



for ssid_name in $(sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null)
do
echo "$ssid_name"
done


I got output like this:



Yash
Shah
CoreFragment
CoreFragment_5G
CoreFragment
Yash
Shah
REDWING
LABS
COMFAST
Appbirds_Technologies
SYMBIOSIS
CoreFragment_5G
REDWING
LABS_5G


Note: When there is a space in output it take as a new line.



I'm working on Ubuntu 18.04.







shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 16 at 18:23









wjandrea

474413




474413










asked Jan 16 at 8:01









yash shahyash shah

649




649













  • Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368

    – Haxiel
    Jan 16 at 8:20











  • i didn't find that (might be due to word match) so I created question

    – yash shah
    Jan 16 at 8:23











  • No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.

    – Haxiel
    Jan 16 at 8:25











  • yaa... it's really useful link

    – yash shah
    Jan 16 at 8:40



















  • Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368

    – Haxiel
    Jan 16 at 8:20











  • i didn't find that (might be due to word match) so I created question

    – yash shah
    Jan 16 at 8:23











  • No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.

    – Haxiel
    Jan 16 at 8:25











  • yaa... it's really useful link

    – yash shah
    Jan 16 at 8:40

















Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368

– Haxiel
Jan 16 at 8:20





Not a duplicate, but the whitespace problem is discussed here: unix.stackexchange.com/q/9496/173368

– Haxiel
Jan 16 at 8:20













i didn't find that (might be due to word match) so I created question

– yash shah
Jan 16 at 8:23





i didn't find that (might be due to word match) so I created question

– yash shah
Jan 16 at 8:23













No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.

– Haxiel
Jan 16 at 8:25





No problem. I wanted to link to that QA because it contains several comprehensive answers that go into the details of that problem.

– Haxiel
Jan 16 at 8:25













yaa... it's really useful link

– yash shah
Jan 16 at 8:40





yaa... it's really useful link

– yash shah
Jan 16 at 8:40










3 Answers
3






active

oldest

votes


















8
















First, note that it is not one command behaving differently. In your code snippets, standard output is written by two quite different commands.

Your first one prints sed's output, while in your second one:




  1. The output of sed is substituted (by the command substitution $(...)) after in;

  2. The resulting string is expanded into a list of elements;

  3. Each item is in turn assigned to ssid_name and printed by echo.


Expansion in point 2 is performed according to the value of the internal field separator (IFS), which by default is <space><tab><newline>. Thus, your solution works because it lets for split the substituted string on newlines only.



As an aside on your answer, note that $'n' is not portable - even if that syntax ($'string') is supported in many shells.

You can nevertheless use an assignment like this:



IFS='
'


An alternative construct for processing lines of input is using read in a while loop:



sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
while IFS= read -r i; do
printf '%sn' "$i"
done


Here, too, each line of sed's output is split according to the value of IFS - that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read regardless of IFS.






share|improve this answer

































    3














    Better use a while loop with read instead of a for loop.



    sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
    do
    echo $ssid_name
    done


    see also https://mywiki.wooledge.org/BashFAQ/001






    share|improve this answer































      1














      By adding following line in code before for loop, work perfactly in my case.



      IFS=$'n'





      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%2f494754%2fcommand-not-working-properly-in-shell-script%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        8
















        First, note that it is not one command behaving differently. In your code snippets, standard output is written by two quite different commands.

        Your first one prints sed's output, while in your second one:




        1. The output of sed is substituted (by the command substitution $(...)) after in;

        2. The resulting string is expanded into a list of elements;

        3. Each item is in turn assigned to ssid_name and printed by echo.


        Expansion in point 2 is performed according to the value of the internal field separator (IFS), which by default is <space><tab><newline>. Thus, your solution works because it lets for split the substituted string on newlines only.



        As an aside on your answer, note that $'n' is not portable - even if that syntax ($'string') is supported in many shells.

        You can nevertheless use an assignment like this:



        IFS='
        '


        An alternative construct for processing lines of input is using read in a while loop:



        sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
        while IFS= read -r i; do
        printf '%sn' "$i"
        done


        Here, too, each line of sed's output is split according to the value of IFS - that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read regardless of IFS.






        share|improve this answer






























          8
















          First, note that it is not one command behaving differently. In your code snippets, standard output is written by two quite different commands.

          Your first one prints sed's output, while in your second one:




          1. The output of sed is substituted (by the command substitution $(...)) after in;

          2. The resulting string is expanded into a list of elements;

          3. Each item is in turn assigned to ssid_name and printed by echo.


          Expansion in point 2 is performed according to the value of the internal field separator (IFS), which by default is <space><tab><newline>. Thus, your solution works because it lets for split the substituted string on newlines only.



          As an aside on your answer, note that $'n' is not portable - even if that syntax ($'string') is supported in many shells.

          You can nevertheless use an assignment like this:



          IFS='
          '


          An alternative construct for processing lines of input is using read in a while loop:



          sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
          while IFS= read -r i; do
          printf '%sn' "$i"
          done


          Here, too, each line of sed's output is split according to the value of IFS - that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read regardless of IFS.






          share|improve this answer




























            8












            8








            8









            First, note that it is not one command behaving differently. In your code snippets, standard output is written by two quite different commands.

            Your first one prints sed's output, while in your second one:




            1. The output of sed is substituted (by the command substitution $(...)) after in;

            2. The resulting string is expanded into a list of elements;

            3. Each item is in turn assigned to ssid_name and printed by echo.


            Expansion in point 2 is performed according to the value of the internal field separator (IFS), which by default is <space><tab><newline>. Thus, your solution works because it lets for split the substituted string on newlines only.



            As an aside on your answer, note that $'n' is not portable - even if that syntax ($'string') is supported in many shells.

            You can nevertheless use an assignment like this:



            IFS='
            '


            An alternative construct for processing lines of input is using read in a while loop:



            sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
            while IFS= read -r i; do
            printf '%sn' "$i"
            done


            Here, too, each line of sed's output is split according to the value of IFS - that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read regardless of IFS.






            share|improve this answer

















            First, note that it is not one command behaving differently. In your code snippets, standard output is written by two quite different commands.

            Your first one prints sed's output, while in your second one:




            1. The output of sed is substituted (by the command substitution $(...)) after in;

            2. The resulting string is expanded into a list of elements;

            3. Each item is in turn assigned to ssid_name and printed by echo.


            Expansion in point 2 is performed according to the value of the internal field separator (IFS), which by default is <space><tab><newline>. Thus, your solution works because it lets for split the substituted string on newlines only.



            As an aside on your answer, note that $'n' is not portable - even if that syntax ($'string') is supported in many shells.

            You can nevertheless use an assignment like this:



            IFS='
            '


            An alternative construct for processing lines of input is using read in a while loop:



            sudo iwlist scan 2>/dev/null | grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null |
            while IFS= read -r i; do
            printf '%sn' "$i"
            done


            Here, too, each line of sed's output is split according to the value of IFS - that in this example is set to the null string exactly to prevent any effects of word splitting - but lines would be kept separated by read regardless of IFS.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 16 at 18:13

























            answered Jan 16 at 9:17









            fra-sanfra-san

            1,386214




            1,386214

























                3














                Better use a while loop with read instead of a for loop.



                sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
                do
                echo $ssid_name
                done


                see also https://mywiki.wooledge.org/BashFAQ/001






                share|improve this answer




























                  3














                  Better use a while loop with read instead of a for loop.



                  sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
                  do
                  echo $ssid_name
                  done


                  see also https://mywiki.wooledge.org/BashFAQ/001






                  share|improve this answer


























                    3












                    3








                    3







                    Better use a while loop with read instead of a for loop.



                    sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
                    do
                    echo $ssid_name
                    done


                    see also https://mywiki.wooledge.org/BashFAQ/001






                    share|improve this answer













                    Better use a while loop with read instead of a for loop.



                    sudo iwlist scan 2>/dev/null |grep ESSID | sed 's/.*ESSID:"(.*)".*/1/' 2>/dev/null | while IFS= read -r ssid_name
                    do
                    echo $ssid_name
                    done


                    see also https://mywiki.wooledge.org/BashFAQ/001







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 16 at 8:55









                    BodoBodo

                    5186




                    5186























                        1














                        By adding following line in code before for loop, work perfactly in my case.



                        IFS=$'n'





                        share|improve this answer




























                          1














                          By adding following line in code before for loop, work perfactly in my case.



                          IFS=$'n'





                          share|improve this answer


























                            1












                            1








                            1







                            By adding following line in code before for loop, work perfactly in my case.



                            IFS=$'n'





                            share|improve this answer













                            By adding following line in code before for loop, work perfactly in my case.



                            IFS=$'n'






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 16 at 8:07









                            yash shahyash shah

                            649




                            649






























                                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%2f494754%2fcommand-not-working-properly-in-shell-script%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

                                1300-talet

                                1300-talet

                                Display a custom attribute below product name in the front-end Magento 1.9.3.8